Monday, 2 December 2013

Classpath Setup for Java - Windows

Purpose: Classpath Setup for Java - Windows 7

We all know the importance of CLASSPATH for Java. Setting PATH/CLASSPATH is very easy but some of us facing difficulties. For them I want to contribute from my blog. Following steps will provide how easy we can set CLASSPATH/PATH for Java.


Step 1: Download and install Java if not available on your box. You can download from the following URL:
http://www.oracle.com/technetwork/java/javase/downloads/index.html

Step 2: By default JDK will be install at "C:\Program Files\Java\jdkxxx.xx" location only. Unless we change the location at the time of installation this is the default location.

Step 3: Go to JDK installed location and you will find bin folder and open that one. copy the bin folder path.




Step 4: Right Click on My Computer and Select "Properties". Once the Properties window is opened click on Advanced System Settings.

Step 5: Once Advance System Settings window is opened click on Environment Variables option



Step 6: Once Environment Variables window is opened click on New under System Variables and enter Variable Name as PATH and Variable Values  as Path of Java Bin Folder(previously copied path) and click OK




Step 7: Go to JDK folder -> open lib folder and copy the path

 


Step 8: Repeat the steps 4 and 5. Now click on New under User Variables and enter User Variable as CLASS and Variable Values as Path of the lib folder and click on OK




Step 9:  Click on OK of Environment Variables window and System Properties window

Step 10: Open Command Prompt and run any java program and you are able to successfully complete.








 

Wednesday, 27 November 2013

Executing same selenium script with multiple browsers only or cross browser testing - Opening Browsers Parallel

Purpose: Executing same selenium script with multiple browsers only or cross browser testing - Opening Browsers Parallel

In my previous post I provided one way of executing same script with different browsers. Now I am going to show how to run the same script parallel(opening all the browsers at time)

Only the change is very small but I wontedly keeping the post separately to avoid the confusion.

The parallel attribute on the <suite> tag can take one of following values:

<suite name="My suite" parallel="methods">

parallel="methods": TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.

<suite name="My suite" parallel="tests">

parallel="tests": TestNG will run all the methods in the same <test> tag in the same thread, but each <test> tag will be in a separate thread. This allows you to group all your classes that are not thread safe in the same <test> and guarantee they will all run in the same thread while taking advantage of TestNG using as many threads as possible to run your tests.

<suite name="My suite" parallel="classes">

parallel="classes": TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.

<suite name="My suite" parallel="instances">

parallel="instances": TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.


From the above parameters: we will use parallel="tests" to achieve our goal.

Step 1: We need to write our case with small change by passing a parameter.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class MultiBrowser {
 private WebDriver driver;

 @Parameters("browser")
 @BeforeMethod
 public void setup(String browser)
 {
  if(browser.equalsIgnoreCase("firefox"))
  {
   driver = new FirefoxDriver();
  }
  else if(browser.equalsIgnoreCase("iexplorer"))
  {
// Update the driver path with your location
   System.setProperty("webdriver.ie.driver", "Drivers\\IEDriverServer.exe");
   driver = new InternetExplorerDriver();
  }
  else if(browser.equalsIgnoreCase("chrome"))
  {
// Update the driver path with your location
   System.setProperty("webdriver.chrome.driver", "Drivers\\chromedriver.exe");
   driver = new ChromeDriver();
  }
  driver.manage().window().maximize();
 }

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

 @Test
 public void testMultiBrowser() throws InterruptedException
 {
  driver.get("http://www.google.com");
  Thread.sleep(3000);
 }
}

2. Now we need to create TestNG.xml and write the following code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MultiBrowser">
       <test name="TestFirefox" verbose="10">
              <parameter name="browser" value="firefox" />
              <classes>
                     <class name="MultiBrowser" />
              </classes>
       </test>
       <test name="ChromeTest">
              <parameter name="browser" value="chrome" />
              <classes>
                     <class name="MultiBrowser" />
              </classes>
       </test>
       <test name="IETest">
              <parameter name="browser" value="iexplorer" />
              <classes>
                     <class name="MultiBrowser" />
              </classes>
       </test>
</suite>

3. Now run the code from TestNG.xml

You can observer all the three browsers will open and perform the task at a time.






 

Friday, 22 November 2013

Executing same selenium script with multiple browsers onfly or cross browser testing

Purpose: Executing same selenium script with multiple browsers

Testing in multiple browsers or Browser compatibility Testing: these are the word we always bother while automating our Test Cases. Following code will help how to execute the same script with multiple browsers.

Step 1: We need to write our case with small change by passing a parameter.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class MultiBrowser {
 private WebDriver driver;

 @Parameters("browser")
 @BeforeMethod
 public void setup(String browser)
 {
  if(browser.equalsIgnoreCase("firefox"))
  {
   driver = new FirefoxDriver();
  }
  else if(browser.equalsIgnoreCase("iexplorer"))
  {
// Update the driver path with your location
   System.setProperty("webdriver.ie.driver", "Drivers\\IEDriverServer.exe");
   driver = new InternetExplorerDriver();
  }
  else if(browser.equalsIgnoreCase("chrome"))
  {
// Update the driver path with your location
   System.setProperty("webdriver.chrome.driver", "Drivers\\chromedriver.exe");
   driver = new ChromeDriver();
  }
  driver.manage().window().maximize();
 }

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

 @Test
 public void testMultiBrowser() throws InterruptedException
 {
  driver.get("http://www.google.com");
  Thread.sleep(3000);
 }
}

2. Now we need to create TestNG.xml and write the following code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="MultiBrowser">
       <test name="TestFirefox" verbose="10">
              <parameter name="browser" value="firefox" />
              <classes>
                     <class name="MultiBrowser" />
              </classes>
       </test>
       <test name="ChromeTest">
              <parameter name="browser" value="chrome" />
              <classes>
                     <class name="MultiBrowser" />
              </classes>
       </test>
       <test name="IETest">
              <parameter name="browser" value="iexplorer" />
              <classes>
                     <class name="MultiBrowser" />
              </classes>
       </test>
</suite>

3. Now run the code from TestNG.xml

You can observer all the three browsers will open and perform the task one another.

Sunday, 17 November 2013

Prioritizing Selenium Test cases using TestNG annotations

Purpose: Prioritizing Selenium Test cases using TestNG annotations

Some times we may need to priorities our test cases based on some requirements. Following code will help you all.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class PriorityParam {
 public WebDriver driver;
 public String baseURL;

 @BeforeMethod
 public void setup()
 {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  baseURL = "http://mail.google.com";
 }

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

 @Test(priority=3)
 public void testPriorityParam() throws InterruptedException
 {
  driver.get(baseURL);
  Thread.sleep(3000);
  System.out.println("Priority 3");
 }

 @Test(priority=2)
 public void testPriorityParam1() throws InterruptedException
 {
  driver.get(baseURL);
  Thread.sleep(3000);
  System.out.println("Priority 2");
 }

 @Test(priority=4)
 public void testPriorityParam2() throws InterruptedException
 {
  driver.get(baseURL);
  Thread.sleep(3000);
  System.out.println("Priority 4");
 }

Thursday, 14 November 2013

Different Operations on Combo Box / Dropdown / Select Option using Selenium Web Driver

Purpose: Different Operations on Combo Box / Dropdown using Selenium Web Driver

We always encounter Combo Box / Dropdown / Select options and following example will help you how to perform different operations.

import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class SelectOption {
 public WebDriver driver;
 public String baseURL;

 @BeforeTest
 public void setup()
 {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  baseURL = "http://hscripts.com/tutorials/html/form-combobox.php";
 }

 @AfterTest
 public void tearDown()
 {
  driver.quit();
 }

 @Test
 public void testSelectOption() throws InterruptedException
 {
  driver.get(baseURL);
 
  WebElement select = driver.findElement(By.xpath(".//form[1]/select"));
  WebElement multiSelect = driver.findElement(By.xpath(".//form[3]/select"));
  List<WebElement> selectedValues = new ArrayList<>();
 
  Select dropdown = new Select(select);
  dropdown.deselectAll();
  dropdown.selectByValue("two");
  selectedValues = dropdown.getAllSelectedOptions();
  for(WebElement value: selectedValues)
  {
   System.out.println("Selected Value: "+value.getText());
  }
  Thread.sleep(3000);
 
  dropdown = new Select(multiSelect);
  dropdown.deselectAll();
  dropdown.selectByValue("three");
  dropdown.selectByValue("four");
  selectedValues = dropdown.getAllSelectedOptions();
  for(WebElement value: selectedValues)
  {
   System.out.println("Selected Value: "+value.getText());
  }
  Thread.sleep(3000);
  dropdown.deselectAll();
  Thread.sleep(3000);
 }
}

Wednesday, 16 October 2013

Reading Test Data from Text File and Writing in to Text File

Purpose: Reading Test Data from Text File and Writing in to Text File

Some times we may need to read test data from text file. We can achieve this using simple Java snippet. following code will help you to address this

import java.io.*;
public class ReadTextFile {
    public static void main(String[] args){
        String readFile ="D://test.txt";
        String writeFile ="D://test1.txt";
       
        //reading contents from a text file  
        try{
            InputStream ips=new FileInputStream(readFile);
            InputStreamReader ipsr=new InputStreamReader(ips);
            BufferedReader br=new BufferedReader(ipsr);
            String line;
            br.readLine();
           
            String[] n = null;
            while ((line =br.readLine())!=null){
             n = line.split(",");
             for(int i = 0; i < n.length; i++)
             {
              System.out.print(n[i]);
             }
             System.out.println("\n");
            }
            br.close();
        }      
        catch (Exception e){
            System.out.println(e.toString());
        }
        //writing to a text file
        try {
            FileWriter fw = new FileWriter (writeFile);
            BufferedWriter bw = new BufferedWriter (fw);
            PrintWriter fileOut = new PrintWriter (bw);
           
                fileOut.println ("\n1. Writing Content to text file");
                fileOut.println ("\n2. Writing content to text file");
               
            fileOut.close();
        }
        catch (Exception e){
            System.out.println(e.toString());
        }      
    }
}

Inupt file contents(test.txt):


First Name:, User First Name
Last Name:, User Last Name
User Name:, User Name
Password:, Password
Email:, email
First Name:, User First Name
Last Name:, User Last Name
User Name:, User Name1
Password:, Password1
Email:, email1
 

Sunday, 29 September 2013

Reading an element contnet without using XPath by WebDriver

Purpose: Reading an element contnet without using XPath by WebDriver

Some times we can access an element with class only but the class contans compound values but className will not accept compound values.

In order to handle these situatuions we need to write differently. Following code will help you to handle it.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Webelements {
 static WebDriver driver;
 // Browser startup
 @BeforeClass
 public void setup() {
 
  driver = new FirefoxDriver();
 }
 // Functionality of Test Case
 @Test
 public void testGoogle() throws InterruptedException, IOException {
 
  driver.get("http://quirksmode.org/css/selectors/multipleclasses.html");
//  System.out.println(driver.findElement(By.className("underline small")).getText());
  System.out.println(driver.findElement(By.cssSelector("p[class='underline small']")).getText());
 
  Thread.sleep(3000);
 }
 @AfterClass
 public void tear() {
  driver.close();
 }