I am automating Amazon app through appium. In product description page it uses webview.
But when using getContextHandles() in appium it only returns NATIVE_APP as context.
Even switching directly by using driver.context("WEBVIEW_1") throws exception saying no such context present.
How to switch to webview in amazon app.
public class AmazonTest {
AndroidDriver<MobileElement> driver;
public void setUp() throws MalformedURLException{
//Set up desired capabilities and pass the Android app-activity and app-package to Appium
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("VERSION", "9");
capabilities.setCapability("deviceName","Mi A2");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("appPackage", "com.amazon.mShop.android.shopping");
capabilities.setCapability("appActivity", "com.amazon.mShop.home.HomeActivity");
//Url where appium server is running
URL url = new URL("http://0.0.0.0:4723/wd/hub");
driver = new AndroidDriver<>(url, capabilities);
}
public void amazonTest() throws InterruptedException {
//Skip sign in
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
MobileElement skipSignInButton = (MobileElement) driver.findElement(By.id("com.amazon.mShop.android.shopping:id/skip_sign_in_button"));
skipSignInButton.click();
//Search for IPhone
driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
MobileElement searchTextElement = (MobileElement) driver.findElement(By.id("com.amazon.mShop.android.shopping:id/rs_search_src_text"));
searchTextElement.sendKeys("iPhone X"+"\n");
//click on first result
driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
MobileElement firstResult = (MobileElement) driver.findElement(By.xpath("//android.widget.TextView[contains(#text,'Apple iPhone X (64GB) - Space Grey')]"));
firstResult.click();
//Click on Enter Pincode button
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
MobileElement pincodeButton = (MobileElement) driver.findElement(By.id("com.amazon.mShop.android.shopping:id/loc_ux_gps_enter_pincode"));
pincodeButton.click();
//Enter pincode
driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
MobileElement pinTextBox = (MobileElement) driver.findElement(By.id("com.amazon.mShop.android.shopping:id/loc_ux_pin_code_text_pt1"));
pinTextBox.sendKeys("248002");
MobileElement applyButton = (MobileElement) driver.findElement(By.id("com.amazon.mShop.android.shopping:id/loc_ux_update_pin_code"));
applyButton.click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//Swipe up to show the buy now button
TouchAction ta = new TouchAction(driver);
ta.press(PointOption.point(540, 1420)).moveTo(PointOption.point(540, 200)).release().perform();
Set<String> con = driver.getContextHandles(); //Get all context associated with this app
for(String s : con) { //only showing native app
System.out.println(s);
}
}
//close the application
public void teardown(){
driver.quit();
}
public static void main(String args[]) {
AmazonTest d = new AmazonTest();
//simulating TestNg Flow
try {
d.setUp();
d.amazonTest();
d.teardown();
}catch(Exception e) {
e.printStackTrace();
}
}
}
For hybrid application, you will need a debug application to run automated tests using appium. If you use the release build for hybrid app, you will be able to automate native context. But to run the non native content you will need a debug app.
Native app : Debug app not needed.
Hybrid app : Debug app is needed to detect web elements, native content can be used both ways.
Related
After the code clicks the action to login, the page gets refreshed and doesn't redirect to the next page. This only occurs for this website as I am redirected properly on other sites.
When I am giving the wrong credentials even the error message is not getting displayed.(Manually it works)
I am testing in Chrome with Selenium java.
Here is my code:
public class Test {
private static final String HTMLPageSourceCode = null;
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","C:\\Selenium project\\chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://-----/tandem/login");
Thread.sleep(5000);
driver.manage().deleteAllCookies();
driver.findElement(By.id("login")).sendKeys("----");
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
driver.findElement(By.id("password")).sendKeys("----");
WebElement loginbutton = driver.findElement(By.xpath(".//*[#id='login-button']"));
Actions actions = new Actions(driver);
actions.click(loginbutton).perform();
}
}
To login in into the website https://outhouse.inhouseusa.com/tandem/login/ you need to induce WebDriverWait for the elementToBeClickable() and you can use the following Locator Strategies:
Code Block:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
//options.addArguments("disable-infobars");
WebDriver driver = new ChromeDriver(options);
driver.get("https://outhouse.inhouseusa.com/tandem/login/");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.form-control#login"))).sendKeys("sandy");
driver.findElement(By.cssSelector("input.form-control#password")).sendKeys("sandy");
driver.findElement(By.cssSelector("input.pull-right.button.button-primary#login-button")).click();
I trying to automate a hybrid app using the appium. i had completed the total setup ready and also testing using sample apk file. I facing problem in getting the object properties for my hybrid app. I am not able to inspect ids using Appium inspector or uiautomatorviewer. It shows only one class for my app .
i also need to enable WebView debugging, for making
setWebContentsDebugging Enabled
to true on the WebView class.can any one help me how to do that?
some of the blogs are saying to keep driver.context("web_view"); but i not clear how to get that. please help to to solve this.
thanks.
this is my java class
public class myMavenTest {
private WebDriver driver;
//i think is not the way to do this.so i comented this.
/*public void onCreate(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
if(0 != (getApplicationInfo().flags = ApplicationInfo.FLAG_DEBUGGABLE)){
WebView.setWebContentsDebuggingEnabled(true);
}
}
}*/
#BeforeTest
public void setUp() throws Exception
{
System.out.println("in the setup function");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("deviceName","My Android");
capabilities.setCapability("platformVersion","5.1");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("appPackage","com.mysoftware.testapp");
capabilities.setCapability("appActivity","com.mysoftware.testapp.MainActivity");
try
{
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
//Thread.sleep(10000);
}
catch(MalformedURLException e)
{
e.printStackTrace();
}
}
#Test
public void Loginforsample() throws Exception
{
System.out.println("in the login() function");
//i tried using classname of my app. but it is not recognizing
driver.findElement(By.className("ink-dark")).click();
//After the button clicks need to enter the text
driver.findElement(By.xpath("//need to find xpath'")).sendKeys("My first Automation");
//tried using selendroid.apk works fine here.
/*driver.findElement(By.id("io.selendroid.testapp:id/startUserRegistration")).click();*/
Thread.sleep(10000);
}
#AfterTest
public void tearDown() throws InterruptedException
{
Thread.sleep(10000);
driver.quit();
}
}
Use below the sample Hybrid App code which I have written for a combination of WEBView Native View. Hope it helps for you!
public class Hybrid_App {
public static void main(String[] args) throws MalformedURLException, InterruptedException{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Atom 2x");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "5.1");
capabilities.setCapability("appPackage","***YourHydridAppPkg****");
capabilities.setCapability("appActivity", "****YourlauchableActivity***");
AndroidDriver driver= new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
Thread.sleep(4000);
Set<String> contextHandles = driver.getContextHandles();
Map<String,String> hashMap= new HashMap<String,String>();
for(String contextname:contextHandles){
if(contextname.contains("NATIVE")){
hashMap.put("native", contextname);
}else{
hashMap.put("webview", contextname);
}
}
//native page - Native
driver.context(hashMap.get("native"));
WebDriverWait wait= new WebDriverWait(driver, 50);
WebElement ele_voucher = wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath("//*[#class='android.view.View'][#index='9'][#content-desc='VOUCHERS']")));
System.out.println(ele_voucher.isDisplayed());
Thread.sleep(6000);
ele_voucher.click();
Thread.sleep(9000);
//second page - Native
driver.context(hashMap.get("native"));
Thread.sleep(5000);
driver.findElementByXPath("//*[#class='android.view.View'][#index='17'][#content-desc='REDEEM']").click();
Thread.sleep(8000);
System.out.println("----Third page----"+" uname,pwd element");
//third page - Webview
driver.context(hashMap.get("webview"));
Thread.sleep(6000);
driver.findElementByXPath("//input[#class='x-input-email'][#type='email'][#name='email']").sendKeys("descbatch#gmail.com");
WebElement ele_pwd = driver.findElementByXPath("//input[#class='x-input-password'][#type='password'][#name='password']");
ele_pwd.click();
Thread.sleep(4000);
ele_pwd.sendKeys("12345");
Thread.sleep(6000);
System.out.println("----Third page----"+" Sign in element");
//fourth page - Native
driver.context(hashMap.get("native"));
Thread.sleep(6000);
driver.findElementByXPath("//*[#class='android.view.View'][#index='69'][#content-desc='SIGN IN']").click();
Thread.sleep(6000);
driver.sendKeyEvent(AndroidKeyCode.BACK);
}
}
I am new to Appium.
I am trying to automate an application which on launch asks to allow to access Contact List. I did research on various websites for the solution but couldn`t resole this issue.
Code:
public class TestSuiteBase {
WebDriver driver = null;
//Element Variables
String deviceIdTextbox = "et_Registration_DeviceID";
#BeforeClass
public void setup() throws MalformedURLException, InterruptedException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "4.4.2");
capabilities.setCapability("deviceName", "0ef3c26f");
capabilities.setCapability("browserName", "");
capabilities.setCapability("app", "D:\\Ashish\\AppiumProject\\Mobile Framework\\apk\\androidApplication.apk");
capabilities.setCapability("appPackage", "com.atyati.rbl.mfi");
capabilities.setCapability("appActivity", "com.atyati.rbl.mfi.Activity.SplashScreenActivity");
AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
Thread.sleep(10000);
}
#Test
public void Test() throws Exception {
Thread.sleep(2000);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("appWaitPackage", "com.android.packageinstaller");
capabilities.setCapability("appWaitActivity", ".permission.ui.GrantPermissionsActivity");
System.out.println("Trying to click Allow");
driver.findElement(MobileBy.id("permission_allow_button")).click();
driver.findElement(By.id(deviceIdTextbox)).sendKeys("123456789");
}
#AfterClass
public void tearDown() throws Exception {
driver.quit();
}
Output: My code launches the app but when permission pop-up surfaces, it is unable to click.
Issue: Not able click on permission pop-up.
Query: Do I need to specify "appActivity" before moving to each screen?
Let me know if any other details needed.
I have figured it out where I was going wrong.
Solution: I have to specify the complete resource id for the Edit Textbox
String deviceIdTextbox = "com.atyati.rbl.mfi:id/et_Registration_DeviceID";
Also, it is not needed to specify the activity.
#Test
public void Test() throws Exception {
Thread.sleep(1000);
if(androidDriver!=null){
System.out.println("driver does not == null");
System.out.println("Trying to click Allow");
androidDriver.findElement(By.id(contactListAllowPermissionBtn)).click();
Thread.sleep(1000);
androidDriver.findElement(By.id(deviceIdTextbox)).sendKeys("123456789");
androidDriver.findElement(By.id(registerUserBtn)).click();
} else {
System.out.println("driver == null");
}
}
You can use this JAVA CODE for clicking on allow app permission button as many times it appears. just add the method and in your class file and call the method in your test case. This code is taken from example on THIS POST
public void allowAppPermission(){
while (driver.findElements(MobileBy.xpath("//*[#class='android.widget.Button'][2]")).size()>0)
{ driver.findElement(MobileBy.xpath("//*[#class='android.widget.Button'][2]")).click();
}
}
I am trying to automate the naukri.com application and as a part of that when I launch the site it basically displays some set of pop up windows which needs to be closed before I proceed to login to the application.This specific functionality has been handled by the code where it closes all the pop up windows and when I proceed to click on the login button, the login button link is not identified and script fails.If i comment the pop up window code then the login button is identified.Please find the below code for the same and kindly help me to resolve the issue
public class naukri {
WebDriver driver = new FirefoxDriver();
#Test
public void pagelaunch() throws InterruptedException{
driver.get("http://www.naukri.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String parenthandle = driver.getWindowHandle();
String parent = driver.getWindowHandle();
//close all the pop ups
Set<String> pops=driver.getWindowHandles();
Iterator<String> it =pops.iterator();
while (it.hasNext()){
String popupHandle=it.next().toString();
if(!popupHandle.contains(parent))
{
driver.switchTo().window(popupHandle);
System.out.println("Pop Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
driver.close();
}
}
System.out.println("the system handle is"+parenthandle);
//to click on login button and proceed to login to the application
driver.findElement(By.xpath("//a[#title='Jobseeker Login']")).click();
Thread.sleep(5000);
for(String winhandle:driver.getWindowHandles())
{
driver.switchTo().window(winhandle);
}
driver.findElement(By.xpath("//a[#id='uSel']")).click();
driver.findElement(By.xpath("html/body/div[8]/div[2]/div[2]/form/div[4]/div[2]/input")).sendKeys("anand_qa");
driver.findElement(By.xpath("html/body/div[8]/div[2]/div[2]/form/div[5]/div[2]/input")).sendKeys("test1234");
driver.findElement(By.xpath("//div[8]/div[2]/div[2]/form/div[7]/button")).click();
driver.switchTo().window(parenthandle);
}
}
public class naukri {
WebDriver driver = new FirefoxDriver();
#Test
public void pageLaunch() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.naukri.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String parenthandle = driver.getWindowHandle();
String parent = driver.getWindowHandle();
//close all the pop ups
Set<String> pops=driver.getWindowHandles();
{
Iterator<String> it =pops.iterator();
while (it.hasNext()) {
String popupHandle=it.next().toString();
if(!popupHandle.contains(parent))
{
driver.switchTo().window(popupHandle);
System.out.println("Pop Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
driver.close();
}
}
}
System.out.println("the system handle is"+ driver.switchTo().window(parenthandle).getTitle());
//to click on login button and proceed to login to the application
driver.findElement(By.xpath("//a[#id='login_Layer']")).click();
Thread.sleep(5000);
/*for (String winhandle:driver.getWindowHandles())
{
driver.switchTo().window(winhandle);
}*/
driver.findElement(By.xpath("//a[#id='uSel']")).click();
driver.findElement(By.xpath("//form[#id ='lgnFrm']/div[4]/div[2]/input[#id='uLogin']")).sendKeys("anand_qa");
driver.findElement(By.xpath("//form[#id ='lgnFrm']/div[5]/div[2]/input[#id='pLogin']")).sendKeys("test1234");
driver.findElement(By.xpath("//form[#id='lgnFrm']/div[7]/button")).click();
}
}
#AK17:
Your code had 2 problems
1.You were not switching to parenthandle after closing the pop-up windows, I added the code
driver.switchTo().window(parenthandle);
2.Your locators for username,password and login button were not correct
Working code, try this:
public class naukri {
WebDriver driver = new FirefoxDriver();
#Test
public void pagelaunch() throws InterruptedException{
driver.get("http://www.naukri.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String parenthandle = driver.getWindowHandle();
String parent = driver.getWindowHandle();
//close all the pop ups
Set<String> pops=driver.getWindowHandles();
Iterator<String> it =pops.iterator();
while (it.hasNext()){
String popupHandle=it.next().toString();
if(!popupHandle.contains(parent))
{
driver.switchTo().window(popupHandle);
System.out.println("Pop Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
driver.close();
}
}
System.out.println("the system handle is"+parenthandle);
driver.switchTo().window(parenthandle);
WebDriverWait wait = new WebDriverWait(driver,10);
WebElement login = driver.findElement(By.xpath("//a[#title='Jobseeker Login']"));
wait.until(ExpectedConditions.elementToBeClickable(login));
//to click on login button and proceed to login to the application
driver.findElement(By.xpath("//a[#title='Jobseeker Login']")).click();
Thread.sleep(3000);
for(String winhandle:driver.getWindowHandles())
{
System.out.println("login: "+winhandle);
driver.switchTo().window(winhandle);
}
Thread.sleep(3000);
driver.findElement(By.xpath("//a[#id='uSel']")).click();
driver.findElement(By.xpath(".//*[#id='uLogin']")).sendKeys("anand_qa");
driver.findElement(By.xpath(".//*[#id='pLogin']")).sendKeys("test1234");
driver.findElement(By.xpath(".//*[#id='lgnFrm']/div[7]/button")).click();
//driver.switchTo().window(parenthandle);
}
}
I'm automating a php web page using webdriver and java language. My code was executing and I was able to perform actions on web page, but today only the login method is executing and my second method gets failed everytime I run. I'm so worried why it is happening. Please help, I'm new in automation testing.
public class TestNGClass {
private String baseUrl = "http://test.com/test2/1.4.6.3/public/admin/";
private WebDriver driver = new FirefoxDriver();
#Test
public void login() {
driver.manage().window().maximize();
driver.get(baseUrl);
driver.findElement(By.name("username")).sendKeys("abc");
driver.findElement(By.name("password")).sendKeys("123");
driver.findElement(By.name("login")).submit();
System.out.print("\nCongrats..You have successfully logged in.");
}
#Test
public void createUser() {
String expectedTitle = "User";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle,"Title Not Found!");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//body/div[3]/div[2]/ul/li[2]/a/img")).click();
Error :
java.lang.AssertionError: Title Not Found! expected [User] but found []
This is because you are using Assert.
Assert.assertEquals(actualTitle, expectedTitle,"Title Not Found!");
Make use of Try catch in-order to proceed next step.
try{
Assert.assertEquals(actualTitle, expectedTitle,"Title Not Found!");
}catch (Exception e)
{
}