Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Why Automation?
When we do same activity multiple times, feels boring we can say, feels at any way it is
good. It will works fine, because every day testing same features. In this case we will miss so
many defects. When we know, we need to test certain feature repeatedly better to automate it.
How it helps?
Selenium
QTP
R.F.T
Silk Test
Test Complete and others
JDK
Eclipse IDE
Firefox browser (By default Selenium Support Firefox)
Firepath
Firebug
Selenium WebDriver
1 Make sure that JDK has been installed in your system and path has been set up.
Note: If Everything is set , just verify.
1 JDK installations:
1. JDK for Windows 8
Search in google - JDK for windows 8 and click on first link (Opens download page)
Select Accept Licence Agreement Radio button.
Windowsx86/Windowx64 click based upon system configuration.
For 64 bit click on windowsx64 link wait till download.
Go to downloads - Double click on downloaded jdk and wait.
Click on NEXT
Click on NEXT
Wait till get successfully installed popup and close it.
To verify Go to C:\Program Files\Java Under this Folder JDK will be there.
2 Eclipse Installations:
Go to https://eclipse.org/downloads/
Click on Eclipse IDE for Java Developers
Based on System configuration click on Windows 64-bit/Windows 32-bit
4 Firebug Installations:
5FirePath Installations :
5 WebDriver Installations :
Go to http://www.seleniumhq.org/download/
Click on Download for Java Language
Go to downloads in your system and extract downloaded file.
Copy Extracted file and Past in created workspace
Launch Eclipse and go to created Project
Right click on Project Go to Build PathSelect Configure build path
Click on Libraries click on Add External JARs
File Name You need to select path of Jars which are extracted and stored in
workspace. Exam C:\NewWorkSpace\selenium-2.44.0\libs
Select all jars under that lib and click on open
Once again click on Add External JARs
Now we need to add Selenium Java 2.44.0 Jar ,example
C:\NewWorkSpace\selenium-2.44.0 Select it and click on open.
So successfully we associated WebDriver Jars
For further reference, please visit the following link::
https://www.youtube.com/watch?v=VJbxrBfKTgY
packageorg.openqa.selenium.example;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.firefox.FirefoxDriver;
importorg.openqa.selenium.support.ui.ExpectedCondition;
importorg.openqa.selenium.support.ui.WebDriverWait;
publicclassSelenium2Example {
publicstaticvoidmain(String[] args) {
// Create a new instance of the Firefox driver
// Notice that the remainder of the code relies on the
interface,
// not the implementation.
WebDriver driver = newFirefoxDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Alternatively the same thing can be done like this
// driver.navigate().to("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us
from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
(newWebDriverWait(driver,
10)).until(newExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
returnd.getTitle().toLowerCase().startsWith("cheese!");
}
});
// Should see: "cheese! - Google Search"
System.out.println("Page title is: " + driver.getTitle());
//Close the browser
driver.quit();
}
}
26-6-2015
Locators
There are 8 types of locators id
name
class name
Xpath
CSS selectors
Link Text
Partial Link Text
tag name.
Locators are used to identify the elements present in the web page.
Selenium by default supports Firefox.
29-6-2015
Program to automate gmail login page
package abc;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class s4 {
public static void main(String[] args) throws InterruptedException {
WebDriver driver=new FirefoxDriver();
driver.get("https://www.gmail.com");
driver.findElement(By.id("Email")).sendKeys("quillabc1234");
driver.findElement(By.id("next")).click();
Thread.sleep(20000);
driver.findElement(By.id("Passwd")).sendKeys("quillabc1234mnbv");
}
}
Xpath
3 ways in which Xpath can be written are
1.html tag[@att= ]
2. html tag[text()= ]
3.html tag[contains(@att, )]
Or
html tag[contains(text(), )]
// in Xpath represents entire page
/ in Xpath represents single child
30-6-2015
1.get Text
2.Radio button
3.Checkbox
4.Size
1-7-2015
Program to automate radio button
package abc;
import java.util.List;
import java.util.concurrent.TimeUnit;
import
import
import
import
org.openqa.selenium.By;
org.openqa.selenium.WebDriver;
org.openqa.selenium.WebElement;
org.openqa.selenium.firefox.FirefoxDriver;
driver.manage().timeouts().implicitlyWait(30,
TimeUnit.SECONDS);
driver.get("http://regentsprep.org/regents/core/question
s/question.cfm?
Course=ESCI&TopicCode=01&QNum=1&Wrong=0");
List<WebElement>
elem=(List<WebElement>)
driver.findElements(By.xpath("//input[@type='Radio']"))
;
/*for(int i=1;i<=elem.size();i++)
{
elem.get(i).click();
}*/
}
}
package abc;
import java.util.List;
import java.util.concurrent.TimeUnit;
import
import
import
import
org.openqa.selenium.By;
org.openqa.selenium.WebDriver;
org.openqa.selenium.WebElement;
org.openqa.selenium.firefox.FirefoxDriver;
InterruptedException {
WebDriver driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30,
TimeUnit.SECONDS);
driver.get("http://www.ironspider.ca/forms/checkradio.h
tm");
List<WebElement> elem=(List<WebElement>)
driver.findElements(By.xpath("//input[@type='checkbox
']"));
for(WebElement e1:elem){
Thread.sleep(1000);
e1.click();
}
}
}
Program by using getText()
package abc;
import java.util.concurrent.TimeUnit;
import
import
import
import
org.openqa.selenium.By;
org.openqa.selenium.WebDriver;
org.openqa.selenium.WebElement;
org.openqa.selenium.firefox.FirefoxDriver;
).getText();
System.out.println(a1);
String s3="COURS";
if(a1.equalsIgnoreCase(s3))
{
System.out.println("pass");
}
else{
System.out.println("fail");
}
}
}
Program to work with chrome browser
package abc;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class s1 {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://google.com");
driver.close();
}
}
Program to automate alert
package abc;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class alert {
public static void main(String[] args) throws
InterruptedException {
WebDriver driver=new FirefoxDriver();
//driver.manage().window().maximize();
driver.get("http://www.ldsjournal.com/Help/Contact.asp
x");
driver.findElement(By.id("ctl00_ctl00_ctl00_Main_Center
_MainContentArea_btnCancel")).click();
Alert a1=driver.switchTo().alert();
Thread.sleep(1000);
a1.dismiss();
//a1.accept();
}
}
Program to demonstrate java script executor
package abc;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class s8 {
package abc;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class linktext {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://beginnersbook.com/2013/04/oopsconcepts/");
driver.findElement(By.linkText("Method
overloading")).click();
}
}
Program to automate frames
package abc;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class frames {
public static void main(String[] args) throws
InterruptedException {
WebDriver driver=new FirefoxDriver();
driver.get("http://www.rediff.com/");
//JavascriptExecutor
a1=(JavascriptExecutor)driver();
Thread.sleep(5000);
/*JavascriptExecutor
a1=(JavascriptExecutor)driver;
a1.executeScript("window.scrollBy(0,750)");*/
Thread.sleep(3000);
WebElement
a11=driver.findElement(By.xpath("//iframe[@id='mone
yiframe']"));
Thread.sleep(3000);
driver.switchTo().frame(a11);
driver.findElement(By.xpath("//div[contains(text(),'Get
Quote')]")).click();
Thread.sleep(3000);
}
}
03-07-2015
Program to demonstrate Mouse Move Over
package abc;
import
import
import
import
import
org.openqa.selenium.By;
org.openqa.selenium.WebDriver;
org.openqa.selenium.WebElement;
org.openqa.selenium.firefox.FirefoxDriver;
org.openqa.selenium.interactions.Actions;
}
}
06-07-2015
AutoIt
Program to upload a document
package abc;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class pra7 {
public static void main(String[] args) throws InterruptedException, IOException {
WebDriver driver=new FirefoxDriver();
driver.get("file:///C:/Users/PRLT-05/Desktop/prakat.html"); //url of the file
Thread.sleep(6000);
driver.findElement(By.xpath("//input[@type='file']")).click();
Thread.sleep(6000);
Runtime.getRuntime().exec("C:\\Users\\PRLT05\\Desktop\\pra1\\Prakat123.exe");
Thread.sleep(6000);
driver.quit();
}
}
8-7-2015
Assignments
1.Recognize any 6 elements on the web page and write the program for the same.
2.Program to login the gmail successfully.
3.( with negative credentials for the above question)
9-7-2015
Program on how to read and write data into excel sheet
Excel
package abc;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import
org.apache.poi.openxml4j.exceptions.InvalidFormatExc
eption;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class Excel1 {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
return rcelldata;
}
public static int rowNum(String sheetname){
int rt=0;
try{
FileInputStream fis=new
FileInputStream("C:\\Users\\PRLT05\\Desktop\\reawri1.xlsx");
Workbook wb =
WorkbookFactory.create(fis);
Sheet s = wb.getSheet(sheetname);
rt=s.getLastRowNum();
}
catch(IOException e){
e.printStackTrace();
}
catch(InvalidFormatException e){
e.printStackTrace();
}
return rt;
Workbook wb =
WorkbookFactory.create(fis);
Sheet s = wb.getSheet(sheetname);
Row r = s.getRow(rownum);
Cell c=r.createCell(cellnum);
c.setCellValue(data);
FileOutputStream fos=new
FileOutputStream("C:\\Users\\PRLT05\\Desktop\\reawri1.xlsx");
wb.write(fos);
fos.close();
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
}
}
To install apache poi
1.Download apache poi.
2.choose the first link, then in Binary Distribution choose the zip file and
download.
Readwrite
package abc;
import java.io.PrintStream;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.gargoylesoftware.htmlunit.ElementNotFoundException;
public class ReadWriteExcelData {
package abc;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import
import
import
import
import
import
org.openqa.selenium.By;
org.openqa.selenium.WebDriver;
org.openqa.selenium.WebElement;
org.openqa.selenium.chrome.ChromeDriver;
org.openqa.selenium.firefox.FirefoxDriver;
org.openqa.selenium.support.ui.Select;
package abc;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Refresh {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://www.speedtest.net/");
driver.navigate().refresh();
}
}
TestNG
TestNG is a testing framework developed in the lines of Junit and Nunit, however it
introduces some new functionalities that make it more powerful and easier to use.
TestNG is an open source automated testing framework, where NG stands for Next
Generation.
TestNG is designed to cover all categories of tests.
Installation of TestNG
1.Go to eclipse, select Help
2.Select eclipse marketplace
3.Type TestNG in find and install.
Advantages
1.Supports annotations.
2.TestNG uses more java and OO features.
3.Flexible plug-in API.
Annotations used@Test- Marks a class or a method as a part of the test.
@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.
@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.
@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.
@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.
Usage of @Test annotation
package abc;
import org.testng.annotations.Test;
public class testng {
// test case 1
@Test
public void testCase1() {
System.out.println("in test case 1");
}
// test case 2
@Test
public void testCase2() {
System.out.println("in test case 2");
}
// test case 3
@Test
public void testCase3() {
System.out.println("in test case 3");
}
// test case 4
@Test
public void testCase4() {
System.out.println("in test case 4");
}
}
Usage of @Test,@BeforeMethod,@AfterMethod,@Before
Class,@AfterClass,@BeforeTest,@AfterTest,@BeforeSuit
e,@AfterSuite
package abc;
import
import
import
import
import
import
import
import
import
org.testng.annotations.AfterClass;
org.testng.annotations.AfterMethod;
org.testng.annotations.AfterSuite;
org.testng.annotations.AfterTest;
org.testng.annotations.BeforeClass;
org.testng.annotations.BeforeMethod;
org.testng.annotations.BeforeSuite;
org.testng.annotations.BeforeTest;
org.testng.annotations.Test;
}
@AfterClass
public void afterClass()
{
System.out.println("In after class");
}
@BeforeTest
public void beforeTest()
{
System.out.println("In before test");
}
@AfterTest
public void afterTest()
{
System.out.println("In after test");
}
@BeforeSuite
public void beforeSuite()
{
System.out.println("In before suite");
}
@AfterSuite
public void afterSuite()
{
System.out.println("In after suite");
}
}
15-7-2015
Framework
Types of framework
1.Hybrid framework