Skip to content

Instantly share code, notes, and snippets.

@ckzgraphics
Created November 29, 2018 12:34
Show Gist options
  • Select an option

  • Save ckzgraphics/ef770b61cc2eef041076338939f73f64 to your computer and use it in GitHub Desktop.

Select an option

Save ckzgraphics/ef770b61cc2eef041076338939f73f64 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.net.URL;
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.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.MobileBy;
public class Android_Save_Card {
public static void main(String[] args) {
String USERNAME = "<USERNAME>";
String AUTOMATE_KEY = "<ACCESS_KEY>";
String HUB_URL = "http://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";
AppiumDriver<WebElement> webDriver = null;
JavascriptExecutor js = null;
WebElement ele = null;
String AUT_URL = "https://rsolomakhin.github.io/autofill/";
URL URLObj = null;
DesiredCapabilities caps = null;
try {
URLObj = new URL(HUB_URL);
caps = new DesiredCapabilities();
caps.setCapability("project", "Test Run");
caps.setCapability("build", "Support Automate");
caps.setCapability("name", "Test: Auto Fill Form");
caps.setCapability("browserstack.debug", "true");
caps.setCapability("browserstack.console", "verbose");
caps.setCapability("browserstack.networkLogs", "true");
caps.setCapability("browserName", "android");
caps.setCapability("device", "Samsung Galaxy S8");
caps.setCapability("os_version", "7.0");
caps.setCapability("realMobile", "true");
webDriver = new AndroidDriver<WebElement>(URLObj, caps);
js = (JavascriptExecutor)webDriver;
Thread.sleep(4000);
// OPEN APPLICATION URL
webDriver.get(AUT_URL);
Thread.sleep(5000);
take_screenshot(webDriver);
ele = webDriver.findElement(By.xpath("(.//h3[contains(.,'Credit Card autofill')]/following-sibling::p/button[contains(.,'Simpsons')])[1]"));
js.executeScript("arguments[0].click();", ele);
Thread.sleep(3000);
ele = webDriver.findElement(By.xpath(".//h3[contains(.,'Credit Card autofill')]/following-sibling::form/select[@name='CCExpiresYear']"));
new Select(ele).selectByValue("2022");
ele = webDriver.findElement(By.xpath(".//h3[contains(.,'Credit Card autofill')]/following-sibling::form/input[@name='cvc']"));
ele.sendKeys("123");
ele = webDriver.findElement(By.xpath("(.//h3[contains(.,'Credit Card autofill')]/following-sibling::form/input[@value='Submit'])[1]"));
js.executeScript("arguments[0].click();", ele);
Thread.sleep(5000);
take_screenshot(webDriver);
String current_contect = webDriver.getContext();
System.out.println("Current context :: " + current_contect);
Set<String> context = webDriver.getContextHandles();
System.out.println("PRINT AVAILABLE CONTEXTS:-");
for (String string : context) {
System.out.println("---> " + string);
current_contect = string;
}
webDriver.context("NATIVE_APP");
// CLICK CLOSE
// webDriver.findElement(MobileBy.xpath(".//android.widget.ImageButton[@resource-id='com.android.chrome:id/infobar_close_button']")).click();
// CLICK SAVE
webDriver.findElement(MobileBy.xpath(".//android.widget.Button[@text='SAVE']")).click();
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(webDriver != null){
webDriver.quit();
}
} // TRY END
} // MAIN END
public static void take_screenshot(WebDriver webDriver){
try {
TakesScreenshot scrShot =((TakesScreenshot)webDriver);
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
// File DestFile=new File("/Users/test/Documents/test.png");
// FileUtils.copyFile(SrcFile, DestFile);
} catch (Exception e) {
e.printStackTrace();
}
}
} // CLASS END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment