How to to change IP geographic location in Java JUnit with Appium on LambdaTest
While performing app automation testing with appium on LambdaTest Grid, you may face a scenario where you would like to simulate location of a specific country. You can easily do that by using the lambdatest capability "GeoLocation" and refer the 2-letter country code in the automation script. You can refer to sample test repo here.
The following is an example on how to set geoLocation in the capabilities.
package com.lambdatest;
import io.appium.java_client.MobileBy;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.net.MalformedURLException;
import java.net.URL;
public class android {
String username = System.getenv("LT_USERNAME") == null ? "LT_USERNAME" //Enter the Username here
: System.getenv("LT_USERNAME");
String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "LT_ACCESS_KEY" //Enter the Access key here
: System.getenv("LT_ACCESS_KEY");
public static RemoteWebDriver driver = null;
public String gridURL = "@mobile-hub.lambdatest.com/wd/hub";
public String status = "passed";
@Before
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("build", "JUNIT Native App automation");
capabilities.setCapability("name", "Java JUnit Android Pixel 6");
capabilities.setCapability("platformName", "android");
capabilities.setCapability("deviceName", "Pixel 6"); //Enter the name of the device here
capabilities.setCapability("isRealMobile", true);
capabilities.setCapability("platformVersion","12");
capabilities.setCapability("app","App_ID"); //Enter the App ID here
capabilities.setCapability("deviceOrientation", "PORTRAIT");
capabilities.setCapability("console",true);
capabilities.setCapability("network",true);
capabilities.setCapability("visual",true);
//ADD GEOLOCATION BASED ON COUNTRY CODE
capabilities.setCapability("geoLocation","fr");
try
{
driver = new RemoteWebDriver(new URL("https://" + username + ":" + accessKey + gridURL), capabilities);
}
catch (MalformedURLException e)
{
System.out.println("Invalid grid URL");
} catch (Exception e)
{
System.out.println(e.getMessage());
}
}
@Test
public void testSimple() throws Exception
{
try
{
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(MobileBy.id("color"))).click();
wait.until(ExpectedConditions.elementToBeClickable(MobileBy.id("geoLocation"))).click();;
Thread.sleep(5000);
driver.navigate().back();
wait.until(ExpectedConditions.elementToBeClickable(MobileBy.id("Text"))).click();
wait.until(ExpectedConditions.elementToBeClickable(MobileBy.id("toast"))).click();
status="passed";
}
catch (Exception e)
{
System.out.println(e.getMessage());
status="failed";
}
}
@After
public void tearDown() throws Exception
{
if (driver != null)
{
driver.executeScript("lambda-status=" + status);
driver.quit();
}
}
}
Execute the following commands to install the required dependencies:
mvn clean
The tests can be executed in the terminal using the following command:
mvn test -P android
mvn test -P ios
Info: Your test results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on the π LambdaTest App Automation Dashboard.
- Advanced Configuration for Capabilities
- How to test locally hosted apps
- How to integrate LambdaTest with CI/CD
Visit the following links to learn more about LambdaTest's features, setup and tutorials around test automation, mobile app testing, responsive testing, and manual testing.
The LambdaTest Community allows people to interact with tech enthusiasts. Connect, ask questions, and learn from tech-savvy people. Discuss best practises in web development, testing, and DevOps with professionals from across the globe π
To stay updated with the latest features and product add-ons, visit Changelog
LambdaTest is a leading test execution and orchestration platform that is fast, reliable, scalable, and secure. It allows users to run both manual and automated testing of web and mobile apps across 3000+ different browsers, operating systems, and real device combinations. Using LambdaTest, businesses can ensure quicker developer feedback and hence achieve faster go to market. Over 500 enterprises and 1 Million + users across 130+ countries rely on LambdaTest for their testing needs.
- Run Selenium, Cypress, Puppeteer, Playwright, and Appium automation tests across 3000+ real desktop and mobile environments.
- Real-time cross browser testing on 3000+ environments.
- Test on Real device cloud
- Blazing fast test automation with HyperExecute
- Accelerate testing, shorten job times and get faster feedback on code changes with Test At Scale.
- Smart Visual Regression Testing on cloud
- 120+ third-party integrations with your favorite tool for CI/CD, Project Management, Codeless Automation, and more.
- Automated Screenshot testing across multiple browsers in a single click.
- Local testing of web and mobile apps.
- Online Accessibility Testing across 3000+ desktop and mobile browsers, browser versions, and operating systems.
- Geolocation testing of web and mobile apps across 53+ countries.
- LT Browser - for responsive testing across 50+ pre-installed mobile, tablets, desktop, and laptop viewports
- Got a query? we are available 24x7 to help. Contact Us
- For more info, visit - LambdaTest