Selenium – QeWorks http://qeworks.com Software Testing Learning Hub Thu, 17 Aug 2017 13:05:33 +0000 en-US hourly 1 Data Provider in TestNG using Excel http://qeworks.com/data-provider-testng-using-excel/ Sun, 04 Jun 2017 13:12:22 +0000 http://qeworks.com/?p=7316 Lets take a look at how make your selenium script data driven using Excel. we will see how to write the code step by step.   Step1 : Design the Excel format. we will create the Excel with below format.   Step2: Create a class which will read the excel file and store the data […]

The post Data Provider in TestNG using Excel appeared first on QeWorks.

]]>
Lets take a look at how make your selenium script data driven using Excel. we will see how to write the code step by step.

 

Step1 : Design the Excel format. we will create the Excel with below format.

 

Step2: Create a class which will read the excel file and store the data into map. Key will be Variable name and Value will be value mentioned in excel. I have used the JXL java api to read the excel sheet. you can use any other api for the same. logic will remain same.

package utilityComponents;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class DataReader {
 public static Map<String, String> data = new HashMap<String,String>();
 public static Object[][] dataset;

 public static Object[][] testcase(String TestID) throws IOException, BiffException
 {
 Properties prop = ReadConfig.config("datasheet.properties");
 //Map or = OrReader.objReader("OR");
 // create map to store web elements
 data = new HashMap<String, String>();
 Workbook workbook = Workbook.getWorkbook(new java.io.File("Excel sheet path"));
 
 Sheet sheet = workbook.getSheet("Sheet name");
 int rowcount = sheet.getRows();
 System.out.println("Datasheet inputs");
 
 for(int i=1;i<rowcount;i++)
 {
 

 Cell ObjectName = sheet.getCell(0, i);
 Cell ObjectValue = sheet.getCell(1, i);
 if(ObjectName.getContents()!="")
 {
 data.put(ObjectName.getContents(), ObjectValue.getContents());
 
 System.out.println("Input VariableName: "+ObjectName.getContents()+"Variable Value from Excel::"+ ObjectValue.getContents() );
 
 }
 else
 
 break;
 
 }
 Object[] keys = data.keySet().toArray();
 Object[] values = data.values().toArray();
 dataset = new String[keys.length][values.length];
 for(int i=0;i<keys.length;i++) {
 dataset[0][i] = (String)keys[i];
 dataset[1][i] = (String)values[i];
 }
 return dataset;
 
 }
}

 

Step3: lets now create a test class where we will call this above method and get the data.

package SmokeTestSuite;

import org.testng.annotations.Test;

import jxl.read.biff.BiffException;
import utilityComponents.DataReader;

import org.testng.annotations.BeforeMethod;

import java.io.IOException;
import java.util.Map;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;

public class DataDrivenTest {
 public static Map<String, String> data;
 @Test(dataProvider = "dp")
 public void f(Map<String, String> testData) throws BiffException, IOException {
 System.out.println(testData);
 
 }
 @BeforeMethod
 public void beforeMethod() {
 }

 @AfterMethod
 public void afterMethod() {
 }


 @DataProvider
 public Object[][] dp() throws BiffException, IOException {

 Object[][] dpdataset = (Object[][]) DataReader.testcase("ID0001");
 return dpdataset;
 
 }
 
}

That’s all.. i hope you are able to use these code to make your script data driven.

The post Data Provider in TestNG using Excel appeared first on QeWorks.

]]>
Dataprovider in TestNG http://qeworks.com/dataprovider-in-testng/ http://qeworks.com/dataprovider-in-testng/#comments Fri, 01 Apr 2016 01:09:52 +0000 http://qeworks.com/?p=5493 Dataprovider is of the most efficient way to make your script datadriven by provide the datasets. Dataprovider is an TestNG annotation which facilitate automation tester to define the data in the method or extract data from external files such as Excel sheet. in this post we will see how to use DataProvider in your script. […]

The post Dataprovider in TestNG appeared first on QeWorks.

]]>
Dataprovider is of the most efficient way to make your script datadriven by provide the datasets. Dataprovider is an TestNG annotation which facilitate automation tester to define the data in the method or extract data from external files such as Excel sheet.

in this post we will see how to use DataProvider in your script.

DataProvider using data inside the method:

package SmokeTestSuite;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
public class Dataprovider {
 @Test(dataProvider = "dp")
 public void f(Integer n, String s) {
 
 System.out.println("In:"+ n +" and s "+ s );
 }
 @BeforeMethod
 public void beforeMethod() {
 }
 @DataProvider
 public Object[][] dp() {
 return new Object[][] {
 new Object[] { 1, "a" },
 new Object[] { 2, "b" },
 };
 }
}

 

outcome of the program:

n:1 and s a
n:2 and s b
PASSED: f(1, "a")
PASSED: f(2, "b")

 

i will post code for Dataprovider using external data source like Excel soon.. stay tune.

The post Dataprovider in TestNG appeared first on QeWorks.

]]>
http://qeworks.com/dataprovider-in-testng/feed/ 3
Selenium Lesson 21 – Creating First TestNG-Selenium Script http://qeworks.com/creating-first-testng-selenium-script/ Fri, 19 Feb 2016 15:17:26 +0000 http://qeworks.com/?p=5467 In last lesson we have seen overview and capabilities of TestNG. now we will actually use them and create working TestNG Test Script with Selenium. you can also watch video posted earlier for better understanding.   Prerequisites : you should have Project created in Eclipse and TestNG plugin should be installed Step1: Create TestNG class(Test case) select […]

The post Selenium Lesson 21 – Creating First TestNG-Selenium Script appeared first on QeWorks.

]]>
In last lesson we have seen overview and capabilities of TestNG. now we will actually use them and create working TestNG Test Script with Selenium.

you can also watch video posted earlier for better understanding.

 

Prerequisites : you should have Project created in Eclipse and TestNG plugin should be installed

Step1: Create TestNG class(Test case)

select the package in project explorer >Right click on it > New > Other

testng-step1

 

Step 2: Select TestNG Class

Select Test NG class from the list and Click on Next Button

testng-step2

 

Step3: Configure the TestNG Test Case

Enter Class Name, Select BeforeTest and AfterTest Annotations. and finally click on the Finish button.

Class Name:  it is test case name.

BeforeTest :  this annotation used with method and this method will be executed before each test method in your TestNG Class. lets say if you have 10 test methods ( test cases) in same class then BeforeTest Method will be executed 10 times before test method.

AfterTest: this annotation used with method and this method will be executed after each test method in your TestNG Class. lets say if you have 10 test methods ( test cases) in same class then AfterTest Method will be executed 10 times after test method.

testng-step3

 

you have successfully created the testng test case.

package TestNG;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class FirstTestNGClass {
 @Test
 public void f() {
 }
 @BeforeTest
 public void beforeTest() {
 }
 @AfterTest
 public void afterTest() {
 }
}

Now lets write Selenium code. We will click on link on Qeworks.com.

in beforeTest() we need to add code to invoke the firefox browser and launch Qeworks.com. method will look like this.

@BeforeTest
public void beforeTest() {
driver = new FirefoxDriver(); // create new instance of firefox driver
baseUrl = "http://qeworks.com/"; // define baseUrl variable
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); // manage timeout
}

in Test() method we will add code to launch Qeworks and click on one of Selenium menu link.

@Test
public void f() {
driver.get(baseUrl + "/");
driver.findElement(By.Xpath("//*[@id='menu-item-5014']/a")).click();
}

in aftertest(), now we have close the opened firefox driver.

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

So after including all variables at class level and combine all above methods, our code will look this this.

package TestNG;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class FirstTestNGClass {
 public static WebDriver driver;
 public static String baseUrl;
 @Test
 public void f() {
 driver.get(baseUrl + "/");
 driver.findElement(By.xpath("//*[@id='menu-item-5014']/a")).click();
 }
 @BeforeTest
 public void beforeTest() {
 driver = new FirefoxDriver(); // create new instance of firefox driver
 baseUrl = "http://qeworks.com/"; // define baseUrl variable
 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); // manage timeout
 }@AfterTest
 public void afterTest() {
 driver.quit();
 }

}

The post Selenium Lesson 21 – Creating First TestNG-Selenium Script appeared first on QeWorks.

]]>
Selenium Lesson 20 – Introduction to TestNG http://qeworks.com/selenium-lesson-20-introduction-to-testng/ Tue, 16 Feb 2016 16:07:23 +0000 http://qeworks.com/?p=5454 TestNG is a testing framework developed in the lines of JUnit and NUnit. It is mainly use to execute the Unit test cases and now a days automated test cases. It has more advance features than Junit and NUnit. TestNG provides lot of annotations to simple use and more flexibility in test execution.   Now […]

The post Selenium Lesson 20 – Introduction to TestNG appeared first on QeWorks.

]]>
TestNG is a testing framework developed in the lines of JUnit and NUnit. It is mainly use to execute the Unit test cases and now a days automated test cases. It has more advance features than Junit and NUnit.

TestNG provides lot of annotations to simple use and more flexibility in test execution.

 

Now a days TestNG become most popular test execution framework in test automation. most used annotation are below.

 

Annotation Description
@BeforeSuite The annotated method will be run only once before all tests in this suite have run.
@AfterSuite The annotated method will be run only once after all tests in this suite have run.
@BeforeClass The annotated method will be run only once before the first test method in the current class is invoked.
@AfterClass The annotated method will be run only once after all the test methods in the current class have run.
@BeforeTest The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
@AfterTest The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.
@BeforeGroups The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
@AfterGroups The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
@BeforeMethod The annotated method will be run before each test method.
@AfterMethod The annotated method will be run after each test method.
@DataProvider Marks a method as supplying data for a test method. The annotated method must return an Object[ ][ ], where each Object[ ] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.
@Factory Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[ ].
@Listeners Defines listeners on a test class.
@Parameters Describes how to pass parameters to a @Test method.
@Test Marks a class or a method as a part of the test.

 

most popular annotations are

  1. @Test
  2. @Dataprovider
  3. @BeforeTest
  4. @AfterTest
  5. @Parameters

 

in next post we will learn how to use these annotation and execute the test case using TestNG.

The post Selenium Lesson 20 – Introduction to TestNG appeared first on QeWorks.

]]>
Selenium Tutorials: Create TestNG Test Case – Video http://qeworks.com/selenium-tutorials-create-testng-test-case/ Sun, 27 Sep 2015 06:32:16 +0000 http://qeworks.com/?p=5292 The post Selenium Tutorials: Create TestNG Test Case – Video appeared first on QeWorks.

]]>

The post Selenium Tutorials: Create TestNG Test Case – Video appeared first on QeWorks.

]]>
Selenium Tutorials: Create Junit Test Case – Video http://qeworks.com/selenium-tutorials-create-junit-test-case-video/ Sun, 27 Sep 2015 06:29:34 +0000 http://qeworks.com/?p=5288 The post Selenium Tutorials: Create Junit Test Case – Video appeared first on QeWorks.

]]>

The post Selenium Tutorials: Create Junit Test Case – Video appeared first on QeWorks.

]]>
Selenium Tutorials : Create Project in Eclipse – Video http://qeworks.com/selenium-tutorials-create-project-in-eclipse-video/ Sun, 27 Sep 2015 06:20:59 +0000 http://qeworks.com/?p=5281  

The post Selenium Tutorials : Create Project in Eclipse – Video appeared first on QeWorks.

]]>

 

The post Selenium Tutorials : Create Project in Eclipse – Video appeared first on QeWorks.

]]>
Execute JavaScript code using Selenium Webdriver http://qeworks.com/execute-javascript-code-using-selenium-webdriver/ Sun, 04 Jan 2015 09:01:05 +0000 http://qeworks.com/?p=4795 Selenium Webdriver provides JavascriptExecutor interface to execute the Javascript on the browser window. this is one of good feature of selenium which allows you to execute client side javascript. So lets create code for it. in this code we will cast the JavascriptExecutor to selenium webdriver. public void exexutejavascript() { driver.get ("url name"); JavascriptExecutor jscript = […]

The post Execute JavaScript code using Selenium Webdriver appeared first on QeWorks.

]]>
Selenium Webdriver provides JavascriptExecutor interface to execute the Javascript on the browser window. this is one of good feature of selenium which allows you to execute client side javascript.

So lets create code for it. in this code we will cast the JavascriptExecutor to selenium webdriver.

public void exexutejavascript()
{
driver.get ("url name");
JavascriptExecutor jscript = (JavascriptExecutor) driver;
// now you are ready to execute the javascript. following JavaScript will return the Title of window. this is just an example.
String winTitle = (String) jscript.executeScript("return document.title");
}

The post Execute JavaScript code using Selenium Webdriver appeared first on QeWorks.

]]>
Identifying and Handling the Frames with Selenium Webdriver http://qeworks.com/identifying-and-handling-the-frames-with-selenium-webdriver/ Sun, 04 Jan 2015 04:00:49 +0000 http://qeworks.com/?p=4793 Many Webpages usually has frames embedded in it. Selenium provides the unique way to identify and handle the frames. Html Frames or Iframes helps to represent the content in different sections of the webpage and also helps in processing certain section of page, instead of reloading the entire page again and again. we can identify […]

The post Identifying and Handling the Frames with Selenium Webdriver appeared first on QeWorks.

]]>
Many Webpages usually has frames embedded in it. Selenium provides the unique way to identify and handle the frames. Html Frames or Iframes helps to represent the content in different sections of the webpage and also helps in processing certain section of page, instead of reloading the entire page again and again.

we can identify the frame using following tags

  • By ID
  • By Name
  • By Index

lets take an example and then we will write a code for it.

lets say i have a page with three frames in it. my html code of the page will look like this…

....
<iframe id ="center", src="demo_iframe.htm" width="200" height="200"></iframe>
<iframe name ="loginwindow", src="demo_iframe.htm" width="200" height="200"></iframe>
<iframe src="demo_iframe.htm" width="200" height="200"></iframe>
.....

 

now 1st frame has ID tag, 2nd Frame has Name tag and 3rd Frame do not have any tag for identification.

lets write a code to identify the frames and switch to frame and come back to main html page.

public void handlingIframes()
{
driver.get ("url name");
// Switch to 1st frame.frame method will idenfity the frame using id attribute provided.
driver.switchTo().frame("center");
// now you can perform any operation on this frame. write code here.
// switch back to parent html page.
driver.switchTo().defaultContent();
// Switch to 2nd frame.frame method will idenfity the frame using name attribute provided.
driver.switchTo().frame("loginwindow");
// now you can perform any operation on this frame. write code here.
// switch back to parent html page.
driver.switchTo().defaultContent();
// Switch to 3rd frame.frame method will idenfity the frame using Index provided.
driver.switchTo().frame(2);
// now you can perform any operation on this frame. write code here.
// switch back to parent html page.
driver.switchTo().defaultContent();
}

The post Identifying and Handling the Frames with Selenium Webdriver appeared first on QeWorks.

]]>
Perform Drag and Drop operations using Selenium Webdriver http://qeworks.com/perform-drag-and-drop-operations-using-selenium-webdriver/ Sat, 03 Jan 2015 11:28:53 +0000 http://qeworks.com/?p=4791 Selenium provides the Actions class perform certain actions which are not posible using simple selenium commands. in this post we will look one of those action. we will write a code to drag and drop the elements from section to other using Actions class. lets write a code to implement the drag and drop actions. […]

The post Perform Drag and Drop operations using Selenium Webdriver appeared first on QeWorks.

]]>
Selenium provides the Actions class perform certain actions which are not posible using simple selenium commands. in this post we will look one of those action. we will write a code to drag and drop the elements from section to other using Actions class.

lets write a code to implement the drag and drop actions.

public void dragDrop()
{
driver.get ("url name");
WebElement Source = driver.findelement(By.xpath("xpath or path of element which you want to drag FROM"));
WebElement target= driver.findelement(By.xpath("xpath or path of element which you want to drag TO"));
Actions builder = new Actions(driver);
builder.dragAndDrop(source, target).perform();
try
{
asserts.Equals("Dropped!", Target.getText());
}
catch(error e)
{
 verificationErrors.append(e.toString());
}
}

The post Perform Drag and Drop operations using Selenium Webdriver appeared first on QeWorks.

]]>