Friday, October 31, 2014


In this tutorial, We will learn how to go forward to the next web page.


Forward To The Next Web Page


1. Go to "http:// www.facebook. com".

2. Click on forgot password link.

3. Come back to "http:// www.facebook. com".

4. Now forward to the next web page (i.e forgot password page).





import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SeleniumForward {

 public static void main(String[] args) {

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to("http://  www.facebook. com");
  WebElement element = driver.findElement(By.linkText("Forgot your password?"));
  element.click();
  driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  driver.navigate().back();
  driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  driver.navigate().forward();

 }

}






Suppose we are in login page and then we clicked on forget password link. Now we are in forget password page and we want to go back to the login page.

Let's see how can we go back to the previous web page.

driver.navigate().back() - This command is used to go previous page.





Back To The Previous Web Page Example


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SeleniumBack {

 public static void main(String[] args) {

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to("http://  www.facebook. com");
  WebElement element = driver.findElement(By.linkText("Forgot your password?"));
  element.click();
  driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  driver.navigate().back();

 }

}






Sometimes we need to refresh the current web page to reload or to get the new dynamic value.

 driver.navigate().refresh() - This command is used to refresh the current page.

Refresh Current Web Page Example






import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SeleniumRefresh {

 public static void main(String[] args) {

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to("http:// www.google. com");
  WebElement element = driver.findElement(By.name("q"));
  element.sendKeys("Selenium");
  driver.navigate().refresh();

 }

}





Tuesday, October 28, 2014


In this tutorial, I will show you how to resolve WebDriver Exceptions.

1. Firefox - You may have faced org.openqa.selenium.firefox.NotConnectedException error.

To resolve this error, we need to download new version of Selenium. You can download Selenium from "http:// docs.seleniumhq. org/download/" and then extract the Selenium folder. Add all the JARs files from Selenium folder to your project.


2. Chrome - You may have faced java.lang.IllegalStateException error for chrome.

To resolve this error, You need to download chrome browser driver from "http:// www.seleniumhq. org/download/" and then extract. 







Add few lines of code -

System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");       
WebDriver driver = new ChromeDriver();

3. Internet Explorer - You may have faced java.lang.IllegalStateException error for Internet Explorer.


To resolve this error, You need to download Internet Explorer browser driver from http:// www.seleniumhq. org/download/ and then extract.

Add few lines of code -

        System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe");  
    
        DesiredCapabilities cap
abilities = DesiredCapabilities.internetExplorer();
        cap
abilities.setCapability("ignoreZoomSetting", true);

        WebDriver driver = new InternetExplorerDriver(capabilities);






Monday, October 27, 2014


In my earlier posts, we have learned WebDriver's Drivers, WebDriver's Commands. In this tutorial, we will learn how to navigate on specific page or URL.

1. get(String URL) - This method will load a web page of given URL in the current browser.

Example: driver.get(“www.google.com”);


2. driver.navigate().to(String URL) - This method will load a web page of given URL in the current browser.

Example: driver.navigate().to("http://www.google.com");


Difference between get() and navigate()


Both navigate().to() and get() do exactly the same thing but with navigate we can go to the next page OR come back to the previous page OR refresh the current page.

1. driver.navigate().forward() - This command is used to go next page. Click here for example.

2. driver.navigate().back() - This command is used to go previous page. Click here for example.

3. driver.navigate().refresh() - This command is used to refresh the current page. Click here for example.







WebDriver Navigate() Example



import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class NavigateExample {

 public static void main(String[] args) {

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to("http://www.google.com");
  WebElement element = driver.findElement(By.name("q"));
  element.sendKeys("Selenium");
  element.submit();

 }

}






Let's see how to list excepted exceptions that a test method can throw. If the test method throw no exception or throw other than excepted exceptions then the test will be marked a failure.

In the below example, there are two test methods -

1. exceptionTest - This test method result will be pass because the actual and expected exceptions is same (i.e. ArithmeticException).

2. exceptionTest1 - This test method result will be fail because the actual and expected exceptions is different.





Example Of TestNG expectedExceptions


import org.testng.annotations.Test;

public class ExceptedExceptionTest {

 @Test(expectedExceptions = ArithmeticException.class)
 public void exceptionTest(){
  int i = 6 / 0;
  
 }
 
 @Test(expectedExceptions = ArithmeticException.class)
 public void exceptionTest1(){
  int i = 65675.7676 / 2;
  
 }
 

}


Output:







Saturday, October 25, 2014


In this tutorial, We will see how to set maximum time for test case execution. If the test case takes more time than the specified time to finish then the TestNG will market it as failed.

To set maximum time for test case execution, we use timeOut attribute of @Test annotation.





TestNG timeOut Example


import org.testng.annotations.Test;

public class TimeOutTestCase {

 @Test
 public void LoginTest(){
  System.out.println("LoginTest test case is Passed");
 }
 
 // time in mulliseconds
 @Test(timeOut = 5000)
 public void WorkflowTest(){
  // System.out.println(" Ignore this test case.");
  try{
   Thread.sleep(6000);
  }catch(Exception e){
   System.out.println(e);
  }
 }
 
 @Test
 public void LogoutTest(){
  System.out.println("LogoutTest test case is Passed");
  System.out.println(" ");
 }

}

Output:









In this tutorial, we will see how to ignore test case. Sometimes, We need to ignore test cases. In this example, We will ignore WorkflowTest test case.

TestNG comes with annotation @Test and attribute "enabled". We need to make enabled equal to false.





TestNG Ignore Test Case Example


import org.testng.Assert;
import org.testng.annotations.Test;

public class IgnoreTestCase {

 @Test
 public void LoginTest(){
  System.out.println("LoginTest test case is Passed");
 }
 
 @Test(enabled = false)
 public void WorkflowTest(){
  System.out.println(" Ignore this test case.");
 }
 
 @Test
 public void LogoutTest(){
  System.out.println("LogoutTest test case is Passed");
  System.out.println(" ");
 }

}

Output:







Thursday, October 23, 2014




In this tutorial, I will list out Selenium WebElement methods. WebElement in a web page is nothing but a text box, button, links, checkbox, radio button, table, window, frames etc.


Selenium WebElement Methods


 1. clear() - This method is used to clear the value of text box.

Example: driver.findElement(By.name(“username”)).clear();



2. click() - This method is used to click on the selected WebElement.

Example: driver.findElement(By.name(“submit”)).click();



3. getAttribute(java.lang.String name) - This method is used to get the value from the fields like Text box.

Example: driver.findElement(By.className(“Blogercup”)).getAttribute(“username”);



4. getSize() - This method is used to get the size of the WebElement.



5. getTagName() - This method is used to get the tag name of the selected WebElement.





6. getText() - This method is used to get text from the selected WebElement of the web page.

Example:

List<WebElement> element = driver.findElements(By.name(“q”));

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

System.out.println(element.get(i).getText());

}



7. sendKeys(java.lang.CharSequence… keysToSend) - This method is used to type text in the selected WebElement

Example:

WebElement element = driver.findElement(By.name(“q”));

element.sendKeys(“Blogercup”);



8. submit() - This method is used to submit a form.

Example:

WebElement element = driver.findElement(By.name(“q”));

element.submit();








In this tutorial, I will list out Selenium WebElement locator. WebElement’s locators used to find all WebElements within a web page.

Selenium WebElement Locator

 

1. findElements() - This method is used to find all WebElements within a web page with same locator.

Example: List<WebElement> element = driver.findElements(By.name(“q”));



2. findElement() -  This method is used to find the first WebElement within a web page.

Example: WebElement element = driver.findElement(By.name(“q”));



3. By ID - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s id.

Example: WebElement element = driver.findElement(By.id(“q”));



4. By Name - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s name.

Example: WebElement element = driver.findElement(By.name(“q”));



5. By Tag Name - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s tagName.

Example: WebElement frame = driver.findElement(By.tagName(“q”));





6. By XPATH - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s xpath.

Example: WebElement element = driver.findElements(By.xpath(“//input”));



7. By CSS - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s css.

Example: WebElement element = driver.findElement(By.cssSelector(“#q span.text.name”));



8. By Class Name – This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s className.

Example: List<WebElement> element = driver.findElements(By.className(“q”));



9. By Link Text - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s linkText.

Example: WebElement element = driver.findElement(By.linkText(“q”));



10. By Partial Link Text - This is a WebElement’s locator and used to find WebElement within a web page using WebElement’s partialLinkText.

Example: WebElement element = driver.findElement(By.partialLinkText(“q”));








WebDriver is an interface in Selenium. WebDriver provides a set of methods that we use to automate web application for testing purpose.


1. get(String URL) - This method will load a web page of given URL in the current browser.

Example: driver.get(“www.google.com”);


2. getCurrentUrl() – This method will return a string representing URL of the current web page that the browser is looking at.

Syntax: driver.getCurrentUrl();


3. getTitle() - This method will return title of the current web page.

Syntax: driver.getTitle();


4. getPageSource() - This method will provide you the source of the last loaded page.

Syntax: driver.getPageSource();


5. close() - If there is multiple windows opened, this method will close the current window and quit the browser, if the current window is the last window opened.

Syntax: driver.close();


6. quit() - Quits all the windows opened.

Syntax: driver.quit();


7. getWindowHandles() - Return a set of string representing window handles which can be used to iterate over all open windows of this WebDriver instance by passing them to switchTo().WebDriver.Options.window().

Syntax: driver.getWindowHandles();


8. getWindowHandle() - Return a string representing window handle of the main window that uniquely identifies it within this driver instance.

Syntax: driver.getWindowHandle();





9. manage() - Gets the Option interface.

Example: driver.manage().addCookie(cookie);


Selenium WebDriver Commands Example


package testng;
 
import java.util.concurrent.TimeUnit;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
 
public class SeleniumDriver {
 
    public static void main(String[] args) {
 
        System.setProperty("webdriver.ie.driver",
                "C:\\Program Files\\Internet Explorer\\iexplore.exe");
 
        WebDriver driver = new FirefoxDriver();
 
        driver.get("http://www.google.com");
 
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 
        WebElement element = driver.findElement(By.name("q"));
 
        System.out.println("URl of current web page -" + " "
                + driver.getCurrentUrl());
 
        element.sendKeys("Selenium!");
 
        System.out.println("Title of current web page -" + " "
                + driver.getTitle());
 
        System.out.println("Window Handle -" + " " + driver.getWindowHandle());
 
        System.out.println("Page Source -" + " " + driver.getPageSource());
 
        driver.close();
 
    }
}








Selenium WebDriver’s driver (i.e. browser driver) is required to make a direct calls to the browser using browser’s native support for automation.

In selenium, FirefoxDriver, InternetExplorerDriver, ChromeDriver and HtmlUnitDriver is a class which implements WebDriver interface.


Selenium WebDriver’s Driver For Different Browsers


1. Firefox Driver: Let’s see how to create Firefox browser’s driver object -

  • WebDriver driver = new FirefoxDriver();


2. Chrome Driver: Let’s see how to create Chrome browser’s driver object -

  • WebDriver driver = new ChromeDriver();





3. Internet Explorer Driver: Let’s see how to create Internet Explorer browser’s driver object -

  • WebDriver driver = new InternetExplorerDriver();


4. HtmlUnit Driver: Let’s see how to create HtmlUnit browser’s driver object -

  • WebDriver driver = new HtmlUnitDriver();


Selenium WebDriver’s Driver Example


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
 
public class SeleniumDriver {
 
    public static void main(String[] args) {
 
        // Create object of the Firefox driver
        WebDriver driver = new FirefoxDriver();
 
        // Open Google       
        driver.get("http://www.google.com");
 
        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));
 
        // Enter text to search for
        element.sendKeys("Selenium!");
 
    }
}








TestNG provides “@Test” annotation along with attribute “dependsOnMethods”. There may be a requirement to make a test case to depends on other test cases. To make a test case to depends on other test cases, we need to use dependsOnMethods attribute with @Test annotation.

 DependsOnMethods Testng Example

 
There are three test cases named “LoginTest”, “WorkflowTest” and “LogoutTest”.

1. WorkflowTest depends on LoginTest. If LoginTest test case failed then WorkflowTest test case will skipped.

2. LogoutTest depends on LoginTest and WorkflowTest test cases. If LoginTest or WorkflowTest test case failed then LogoutTest test case will skipped.





TestNG DependsOnMethods


import org.testng.Assert;
import org.testng.annotations.Test;
 
public class TestCasePriority {
 
    @Test(priority=1)
    public void LoginTest(){
        Assert.assertEquals("true", "false");
    }
 
    @Test(priority=2, dependsOnMethods={"LoginTest"})
    public void WorkflowTest(){
        System.out.println(" This test case should Skipped, if LoginTest test case Failed");
    }
 
    @Test(priority=3, dependsOnMethods={"LoginTest", "WorkflowTest"})
    public void LogoutTest(){
        System.out.println(" This test case should Skipped, if LoginTest or WorkflowTest test case Failed");
        System.out.println(" ");
    }
 
}








We need to set priority of test cases to execute test case in a particular order. If we do not set priority of test cases then we are not sure the order of execution of test case (i.e. Logout test case may run or execute before Login test case).

Steps to test an application


1. Login

2. Workflow

3. Logout


There is three test cases


1. Login test case

2. Workflow test case

3. Logout test case





How to set priority of test cases


import org.testng.annotations.Test;
 
public class TestCasePriority {
     
    @Test(priority=1)
    public void LoginTest(){
        System.out.println(" Login Test case should execute first");
    }
     
    @Test(priority=2)
    public void WorkflowTest(){
        System.out.println(" Workflow Test case should not execute before Login test case");
    }
     
    @Test(priority=3)
    public void LogoutTest(){
        System.out.println(" Logout should not execute before Login and Workflow test cases");
        System.out.println(" ");
    }
 
}