Monday, May 30, 2016

Parallel testing using @parameter in testNG_Cross browser testing using webdriver

This is very common, in web testing when we need to test our application in different browsers and webdriver and testNG together has made our life easy to achieve this thing.

We all know testNG is very rich in features and one of best thing is , it allow you to do your test parallel. The parallel testing features to run your test in diff browser in same time without wasting the time. we can run same test same time in diff browsers. Let's see how we can achieve this:
We will create a testNG class  e.g. "parallelTesting.java" and the xml file name say it is again "paralleltesting.xml"

Here the xml:
























in this xml we have two test : 1- Test in Firefox and other is 2- Test in Chrome
and in both test we use same class "parallelTesting" .
what is noticable here is tag parameter which has name as browser and value is Firefox and Chrome for test1 and test2 respectively.

Lets take a look in to jave file.
package practiceTestNG;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class parallelTesting
{
      WebDriver driver= null;
   
      @BeforeTest
      @Parameters({"browser"})
    public void sendbrowser(String browser)
        {
            if(browser.equals("Firefox"))
                {
                driver = new FirefoxDriver();
                }
                else if(browser.equals("Chrome"))
                {
                System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
                driver = new ChromeDriver();
                }
         
        }
   
    @Test
    public void blogTitle()
        {
        driver.get("http://www.vishalsachan.blogspot.in");
        String title = driver.getTitle();
        Assert.assertEquals("Simplifying the test", title);
        driver.quit();
           
        }
    }
 here we have  one class with @Test annotation and another class with
@Beforetest which will take parameter as browser which will have values as Firefox and Chrome (in our scenarios).

Now run ur testsuit and u will observe that your test is run in both browser parallel.   We can code change the code if we want to include more browsers. so its all. enjoy  cross browser testing without much pain. 
Stay tuned for more fruitful post.

Using an user defined firefox profile during webdriver test

You must have observed that we we run our test using webdriver, firefox driver open an instance of browser where there are no addons, bookmarks or any other things available. This is because it uses a instance with blank/new profile every time. But what if we need to test in a predefined profile like, any test case prerequisite is that , any addon must be installed in browser, book marks , or any other setting. 

We can do this by using user defined profile. To do so, follow below steps:

1- Close you browser.
2- In start go to run and type "firefox.exe -p" 
3- This will open a window where your profile are listed. Here we can create , modify , delete ect profiles.
4- If you see there will a default profile. we can use that as well for our test.
5- If you need, create a specific profile as required.

Now see how we can use any profile in webdriver.

package practiceTestNG;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class fireforxProfile {

    public static void main(String[] args)
    {
        ProfilesIni p = new ProfilesIni();
        FirefoxProfile fp = p.getProfile("default");
        WebDriver driver = new FirefoxDriver(fp);
        driver.get("https://www.vishalsachan.blogspot.com");
    }

}

 
In above code, you see we have used 'default' profile in our webdriver. now when you run this , you test will be open in a browser where u can see your addons, bookmarks ect.
 

Tuesday, May 24, 2016

Perform mouseover function in Selenium WebDriver using Java

import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class Test1
{

    public static void main(String[] args) throws InterruptedException
    {
       
        FirefoxDriver driver = new FirefoxDriver();
        driver.get("http://flex.apache.org/");
        Actions action = new Actions(driver);
        action.moveToElement(driver.findElement(By.xpath(".//*[@id='nav']/li[2]/a"))).build().perform();
        Thread.sleep(1000);
        action.click(driver.findElement(By.xpath(".//*[@id='nav']/li[2]/ul/li[2]/a"))).perform();

     }
}

Monday, May 23, 2016

WebDriver Explicit Wait: wait for element to load

If you want webdriver to wait for any element to be visible, we can use explicit wait. In simple way iof we use wait function for any given time, we can not make sure how muych time it will take to an element to get visible. Or if you give more time for wait, may be element gets visible early and un necessary your sript wait for the given time. So to manage this, we can use wait in diff way by creating instance variable of WebDriverWait class. This can be as below:

import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Test1 {

       public static void main(String[] args)
       {
             
              FirefoxDriver driver = new FirefoxDriver();
              driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); //This is implicit wait used

              driver.get("http://vishalsachan.blogspot.in");
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.id("as_rst")));
        driver.findElement(By.id("as_rst")).click();
       }
}


In example above, script will wait only in that step  for 30 seconds or until element visible (whatever occurs first) and will move next step.

Monday, May 16, 2016

Working with pdf in selenium webdriver java project


In our test automation, sometimes we need to work with pdf file, iether verifying the text there or to copy some text and storing it to some other file or places. But in Selenium WebDriver, we don’t have any direct methods to do that. 
Don't worry, we can do this using Apache PDFBox. to use this we have to import pdfbox jar file to our selenium java project. This jar, we can get from apache website. or just click here download pdfbox.jar
 

Simply download the latest .jar file and add to your project build path in eclipse.

we are all set to work with pdf now.
Below lets write a simple program where we will read a pdf and print the number of pages in that pdf file. also we will print the content of pdf using stripper class.


package WedriverPackage;
import java.io.File;import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
 

public class workWithPDF
{
    public static void main(String[] args) throws IOException     

    {
      PDDocument A = PDDocument.load(new File("C:/Users/IBM_ADMIN/Desktop/Recognition_vishal.pdf"));
      System.out.println("total number of pages:" + A.getNumberOfPages());
      PDFTextStripper B = new PDFTextStripper();          
      System.out.println(B.getText(A));

    }
}

 

Monday, May 2, 2016

Handling Captcha in WebDriver

As definition itself says:
"A CAPTCHA is a program that  protects  websites against bots  by generating and grading tests that humans can pass but current computer programs cannot"

So these pages can't be automated fully. In fact Captcha itself is implemented to prevent automation. we can not completely automate the page having captcha.

While automation testing we can handle it by 3 ways as given below:
1- By disabling of captchas on test environments.

2- Hard code the captcha.
3- Handle by entering the captcha manually during execution.
 

3rd point can be done using below

WedriverPackage;

import java.util.Scanner;

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

public class HandlingCaptcha 

{    
        public static void main(String[] args)     
          {        
          
               WebDriver driver = new FirefoxDriver();        
               driver.get("https://www.irctc.co.in/");
               driver.findElement(By.id("usernameId")).sendKeys("vishal");
               driver.findElement(By.className("loginPassword")).sendKeys("vishal");  
               Scanner scan = new Scanner(System.in);
               System.out.println("Enter Captcha"); 
               String captcha = scan.nextLine(); 
               driver.findElement(By.className("loginCaptcha")).sendKeys(captcha);   
               driver.findElement(By.id("loginbutton")).click();    
           }
}



In case anyone has any other solution, or way to handle it, please share in comments. Thanks.