Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ckzgraphics/6453c5ee298117c70189b0ae1c57b4f7 to your computer and use it in GitHub Desktop.
Save ckzgraphics/6453c5ee298117c70189b0ae1c57b4f7 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileBy;
import io.appium.java_client.android.AndroidDriver;
public class Android_Popup_WouldLikeToUseYourLocation {
static String USERNAME = "<BROWSERSTACK_USERNAME>";
static String AUTOMATE_KEY = "<BROWSERSTACK_ACCESS_KEY>";
static String HUB_URL = "http://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";
public static void main(String[] args) {
JavascriptExecutor js = null;
AppiumDriver webDriver = null;
// AndroidDriver webDriver = null;
// WebDriver webDriver = null;
String AUT_URL = "https://www.w3schools.com/html/html5_geolocation.asp";
URL URLObj = null;
DesiredCapabilities caps = null;
try {
URLObj = new URL(HUB_URL);
caps = new DesiredCapabilities();
caps.setCapability("device", "Google Pixel");
caps.setCapability("real_mobile", "true");
caps.setCapability("project", "Test Run");
caps.setCapability("build", "Support Automate");
caps.setCapability("name", "Test: Website wants to use your device's location [Android]");
caps.setCapability("browserstack.debug", "true");
caps.setCapability("browserstack.console", "verbose");
webDriver = new AndroidDriver(URLObj, caps);
js = (JavascriptExecutor) webDriver;
webDriver.get(AUT_URL);
Thread.sleep(2000);
take_screenshot(webDriver);
webDriver.findElement(By.xpath("//*[@id='main']/p[2]/button")).click();
Thread.sleep(2000);
take_screenshot(webDriver);
System.out.println("Current :: " + webDriver.getContext());
Set<String> hand = webDriver.getContextHandles();
System.out.println("CONTEXT HANDLE:: " + hand);
String webwin = "";
System.out.println("ALLs HANDLE:: " );
for (String string : hand) {
webwin = string;
System.out.println(" # " + string);
}
webDriver.context("NATIVE_APP");
webDriver.findElement(MobileBy.xpath(".//android.widget.Button[@text='Allow']")).click();
Thread.sleep(5000);
take_screenshot(webDriver);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(webDriver != null){
webDriver.quit();
}
} // FINALLY END
}
public static void take_screenshot(WebDriver webDriver){
try {
TakesScreenshot scrShot =((TakesScreenshot)webDriver);
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
} catch (Exception e) {
System.out.println("take_screenshot :: ERROR STORING SCREENSHIOT :: " + e.getMessage());
// e.printStackTrace();
}
} // take_screenshot END
} // CLASS END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment