Thursday, May 28, 2015

Running your first Selenium Webdriver script with TestNG


Running your first Selenium Webdriver script with TestNG

WebDriver is a tool for automating testing web applications, and in particular to verify that they work as expected. It aims to provide a friendly API that's easy to explore and understand, which will help make your tests easier to read and maintain. It's not tied to any particular test framework, so it can be used equally well with JUnit, TestNG or from a plain old "main" method.

You can use any IDE for developing your scripts but I like using Eclipse. I will be explaining later how to use Eclipse.

Ok...so lets start creating the
Selenium Webdriver script using TestNG. You will need to install the following first.

Install the below
1. JDK : You can be download it from here 
http://java.sun.com/javase/downloads/index.jsp. Set the classpath.

2. Selenium Webdriver: Download it from here: 
http://seleniumhq.org/download . Extract the files in a folder named Selenium_Webdriver  





3. TestNG : Download TestNG from http://testng.org/doc/download.html. Unzip. The files will get extracted to a folder called testng.

4. Eclipse : Download eclipse from 
http://www.eclipse.org/downloads/.Choose the "Eclipse IDE for Java Developers".Unzip the downloaded file. The files will get extracted to a folder called 'eclipse'.

5. TestNG plug-in for Eclipse: Start Eclipse by clicking the eclipse icon in the eclipse folder that's mentioned earlier. Click on Help->Install New Softwares. Enter "http://beust.com/eclipse/" in the "Work With:" and press Enter. You should see TestNG. Select it and then press Next till you reach Finish. Restart Eclipse.

Good that we have finished installing everything that we needed to start with selenium and TestNG. You could always work with selenium and testng without eclipse in which case you wont have to install Eclipse and the TestNG plug-in required for eclipse. But its better to have an IDE (like Eclipse or anyother) so that its far easier to write the scripts.

Let's start with the Selenium Webdriver and TestNG script now:

1. Open eclipse. Start by creating a new project : File->New->Java Project. Give a name say 'Automation'. 

2. Add the required jars. Select the project-’Automation’, Right click Build Path->Configure Build Path. Click Libraries->Add External Jars. You will have to add the testng and selenium jars and libs.Also Add the In build TestNG , Junit , Java Libraries from Add Library.

3. Create a new package in the project. Select the project -'Automation' and then right click New->Package. Give a name say 'First'.

4. Create a new java program. Select the package-'First' and then right click New->Class. Enter the name as say ‘GoogleTC’.Press Enter.
5. Copy paste the whole below code into eclipse(GoogleTC).

package First;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Test;

public class GoogleTC {
     
      public WebDriver driver;
     
     
  @Test
  public void f() throws Exception {
        driver = new FirefoxDriver();
        driver.get("http://www.google.com"); // open google.com
      Thread.sleep(2000); // This statement is used to wait for 2 seconds
      driver.manage().window().maximize(); //Maximize the window
      Thread.sleep(5000);
      driver.findElement(By.id("lst-ib")).clear();
      driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
      driver.findElement(By.name("btnG")).click();
      Thread.sleep(2000);
      driver.findElement(By.linkText("Selenium - Web Browser Automation")).click();
             
      driver.close();
      driver.quit();
  }
}



6. Make changes so that your testng script looks like below:

Great!!!!! Your first testng
 script is ready to be run. Wasnt that easy?
This is simple script which search keyword Selenium in google.

7. Now run the testng script by going back to eclipse. Right click on the testng script. Click RunAs->TestNG Test.
Your script should run successfully. 

Tuesday, January 20, 2015

Data Driven in Selenium Webdriver

 


Data driven in Selenium webdriver . Read Data from excel sheet and fill the username and password of gmail.


Source code:

import java.io.FileInputStream;

import jxl.Sheet;
import jxl.Workbook;

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 EmailTest {
private WebDriver driver;

@BeforeClass
public void Startup() {
driver = new FirefoxDriver();
}

@Test(description = "Read Gmail account")
public void Login() throws Exception {
FileInputStream fi = new FileInputStream("E:\\testemail.xls");
Workbook w = Workbook.getWorkbook(fi);
Sheet s = w.getSheet(0);
driver.get("http://www.gmail.com");
try {
for (int i = 1; i < s.getRows(); i++) {
// Read data from excel sheet
String s1 = s.getCell(0, i).getContents();
String s2 = s.getCell(1, i).getContents();
driver.findElement(By.name("Email")).sendKeys(s1);
driver.findElement(By.name("Passwd")).sendKeys(s2);
driver.findElement(By.name("signIn")).click();
Thread.sleep(9000);

}
} catch (Exception e) {
System.out.println(e);
}
}

@AfterClass
public void teardown() {
driver.quit();
}
}





Monday, January 12, 2015

Display Alert Message of Test case

We can display alert message in screen to know which test case is executing or to show user of respective action.



Method to display alert Message of test case -

public void Show_ScriptMessage_Close(String Message , int time) throws Exception
  {
     
      time = time * 1000; // 1 value to 1000
       final ImageIcon icon = new ImageIcon("C:\\Selenium\\Test_data\\prathip.png");
        final JOptionPane optionPane = new JOptionPane(Message, JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, icon , new Object[]{}, null);

        final JDialog dialog = new JDialog();
        dialog.setTitle("Selenium Scripting - Prathip");
       
        dialog.setModal(true);
       
        dialog.setLocationRelativeTo(null);
        dialog.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width)/2 - dialog.getWidth()/2, (Toolkit.getDefaultToolkit().getScreenSize().height)/2 - dialog.getHeight()/2);
        dialog.setContentPane(optionPane);

        dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
         
     
        dialog.pack();
       
      //create timer to dispose of dialog after 5 seconds
        // 1 sec = 1000
        Timer timer = new Timer(time, new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                dialog.dispose();
            }
        });
        timer.setRepeats(false);//the timer should only go off once

        //start timer to close JDialog as dialog modal we must start the timer before its visible
        timer.start();

        dialog.setVisible(true);
     
  }




how to call the function -

Show_ScriptMessage_Close("Google Search" , 5);