Wednesday 24 July 2013

How to handle StaleElementReferenceException?


Why StatleElementReferenceException is thrown?


StatleElementReferenceException indicates that a reference to an element is stale, means that element no longer appears on the DOM of the page.

For example let we consider the following code,

                List <WebElement> alllinks = driver.findElements(By.tagName("a"));

for(WebElement link : alllinks) {
link.click(); // It may cause StatleElementReferenceException 
                         driver.navigate().back();
}

Here the reference variable 'alllinks' has the reference of all the WebElements, those has anchor(//a) tag. Coming to below, the 'for' loop clicks on the each WebElement, then navigates to its corresponding page and comes back to the original page. Due to this navigation/refresh DOM references might be changed. But the reference variable 'alllinks' holds the previous references of WebElements. Now the reference of the element becomes 'stale'. In this case the StaleElementReferenceException is thrown.

How to handle StatleElementReferenceException?



You can avoid this StatleElementReferenceException be having following style of code,

             int sizeOfAllLinks = driver.findElements(By.tagName("a")).size();

            for (int index=0; index<sizeOfAllLinks; i++ ) {
                  getElementWithIndex(By.tagName("a"), index).click();
                  driver.navigate().back();
            }


            public WebElement getElementWithIndex(By by, int index) {
                  return webDriver.findElements(by).get(index);
            }

Convert .apk to java files


1. Change the extension of your .apk file to .zip
2. Then unzip the zip file, there you will see a file, named classes.dex. This file only has all the java class files
3. Now you need to convert the classes.dex file into jar file. We can do it by Dex2Jar. Download the it from the here  and unzip it.
4. Copy your classes.dex file into the unzipped Dex2Jar folder
5. Now go to the command line and go to the path of Dex2Jar folder, then run the below command

            dex2jar.bat classes.dex  ---> This will create a jar file in the unzipped folder.
6. Now you need to convert this jar file into java classes. For the you can use java decompiler. Download that from here

Friday 19 July 2013

How to install .apk file in emulator - windows platform

Open the emulator from the command line


Open the command prompt and go into the 'tools' folder of your Android SDK installed and run the below command,

            emulator -avd devicename

For example I have created an emulator already with the name 'test'. so the command would be as below 

            E:\adt-bundle-windows-x86-20130522\sdk\tools>emulator -avd test.

Install the .apk file into the emulator


Open the new command prompt and go into the 'platform-tools' folder of your Android SDK installed and run the below command,

           adb install apkFileNameWithPath

For example, I installe the myApp.apk into the opened emulator by the below command,

           E:\adt-bundle-windows-x86-20130522\sdk\platform-tools>adb install E:\AndroidWorkspace\appiumandroid\src\Resource\myApp.apk


Friday 12 July 2013

WebDriver TestNG example


public class GmailTestCases {

  @BeforeMethod
public void launch() {
driver= new FirefoxDriver();
  driver.get("http://gmail.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}

@AfterMethod
public void quit() {
driver.quit();
}


@Test
public void verifyLoginPage() {

String actual_title = driver.getTitle();
String expected_title = "Gmail: Email from Google";
Assert.assertEquals(expected_title, actual_title);
}


@Test
public void verifyLoginWithValidCredentials() throws Exception
{
driver.findElement(By.id("Email")).sendKeys("test@gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("validpassword");
driver.findElement(By.id("signIn")).click();
Thread.sleep(10000);
String expected_username = "Test user";
String actual_username   = driver.findElement(By.id("gbi4t")).getText();
Assert.assertEquals(expected_username, actual_username);
}



@Test
public void verifyLoginWithInValidCredentials() throws InterruptedException {
driver.findElement(By.id("Email")).sendKeys("test@gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("invalidpwd");
driver.findElement(By.id("signIn")).click();
Thread.sleep(10000);
                Assert.assertTrue(driver.getPageSource().contains("The username or password you entered is incorrect"));
}

@Test
public void verifyLinks() throws InterruptedException {

List <WebElement> alllinks = driver.findElements(By.tagName("a"));

for(int i=0;i<alllinks.size();i++) {
System.out.println(alllinks.get(i).getText());
}

for(int i=0;i<alllinks.size();i++){

alllinks = driver.findElements(By.tagName("a"));
alllinks.get(i).click();
Thread.sleep(1000);
driver.navigate().to("http://gmail.com");
Thread.sleep(1000);
}
}

================================================================

XML file

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <!-- Specify the test suite name --> <suite name="Gmail_Login" verbose="1" annotations="JDK"> <!-- Specify the test name --> <test name="gmail_Login_Scenarios" preserve-order="true"> <classes> <class name="packagename.GmailTestCases"> <methods> <include name="verifyLoginPage" /> <include name="verifyLoginWithValidCredentials" /> <include name="verifyLoginWithInValidCredentials" /> <include name="verifyLinks" /> </methods> </class> </classes> </test> </suite>




Mouse hover in WebDriver

'Action' class supports for mouse hover functionality as below,


                   WebElement element = driver.findElement(By.id("menu"));
                   Actions builder = new Actions(driver);
                  builder.moveToElement(element).build().perform();





Select value from drop down list using WebDriver

WebDriver provides the class named 'Select' which is in the package 'org.openqa.selenium.support.ui' and below methods to choose the value from the drop down list.

  • selectByVisibleText("text")
  • selectByValue("value")
  • selectByIndex(index number)

Note: This is applicable only if the drop down list implemented by <select> tag  in html as below,

Let us consider the face book sign-up page, which has the dropdown lists in the Birthday field as below,



Example Program:


It chooses month for the Birthday field.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class dropDownList{
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

                driver.get("http://www.facebook.com/");
              
                driver.manage().window().maximize(); 
// Chooses Apr month
Select brtmonth = new Select(driver.findElement(By.id("birthday_month")));
brtmonth.selectByVisibleText("Apr");
}

}




Webdriver example program

Naukri Login:



import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;

public class Login {
public static void main(String args[]) throws InterruptedException {

//Launches the firefox browser
WebDriver driver = new FirefoxDriver();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

                // Loads the Naukri application
driver.get("http://www.naukri.com/");

                // Maximize the browser
                driver.manage().window().maximize();
                // Prints the title of the naukri application
System.out.println(driver.getTitle());
 
                // Enters the username
driver.findElement(By.id("username")).sendKeys("tester@gmail.com");

                // Enters the username
driver.findElement(By.id("password")).sendKeys("tester");

                // Clicks on the login button
driver.findElement(By.name("Login")).click();
Thread.sleep(8000); // Using sleep() is not a good practice, we need to use explicit wait, will see about this in later post

               // Stores the username displayed in the home page into the variable 'actual'
String actual = driver.findElement(By.id("nameDisplaySpan")).getText(); 
System.out.println("actual: "+actual);

                // Clicks on the log out link
driver.findElement(By.linkText("LogOut")).click();


                // Close the browser
                driver.close();

                // Quit the driver session
driver.quit();
}

}