Sei sulla pagina 1di 24

Welcome to Selenium Training

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?

Once we prepared the scripts, we can use n times.(Re useable)


Faster ( Takes less time to execute), so saves time
Increase the quality
Same script as and when need , we can use it Repeatable
We can set timing to execute during night times and morning can check the Report.
To work with automation , it save time , money and increase the quality of product

Test Automation Tools

Selenium
QTP
R.F.T
Silk Test
Test Complete and others

Advantages to work with SeleniumWebDriver:

Support many languages


Support many browser
Support many platform
Its an open source

Selenium:Selenium is an open source automated testing suite of software for web


application across different browser and platform. It has 4 components,
1. Selenium IDE
2. Selenium RC
3. WebDriver
4. Grid

To work with Selenium Automation we need:


1.
2.
3.
4.
5.
6.

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.

1.1. We need to set the path.


Go to My System/System/My Computer and right click on it -> Click Properties
Click on Advance System Settings -> Click on Environment Variable
Search for path in System variable
Select path and click on Edit
Go to C:\Program Files\Java\jdk1.8.0_31\bin copy link till bin.
In variable value go to last and type ;
Paste under variable value and click on ok
To verify it..
Go to Search Type cmd ..>enter. Launch command prompt
Seachjavac in command prompt.
From command prompt get to know, JDK and path is set properly.
1.1.2 JDK for Windows 7
Search in google - JDK for windows 7
Click on Second link (Opens download page)
Please follow other step similar to windows 8
https://www.youtube.com/watch?v=pb-r8U2kpCU (for windows8) (Hari this link for your
Reference)

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

Click on Download and wait till download


Go to Downloads in your system and search downloaded
Extract the Zip file and extracted zip file copy the Eclipse Folder
Paste under C:\Program Files and press continue
Go inside this eclipse and able to see Eclipse icon
Double click on icon
Need to mention workspace (This is the link where we keep our Project Document)
Click on OK.
Once opened Eclipse ..> Go to File >New.>Click on Java Project
Type Project Name SeleniumTraining and click on Finish
Cancel Welcome screen
Go to Created Project
Go to Source folder Src under SeleniumTraining Project
Right click on srcgo to New click on Class
Type name of the class as Sample Program and select Public static void main
check box and click on finish
Write sample code under created class as Sytem.out.println(Hello Selenium);
Save and Run it.
In console you can see Hello Selenium
So successfully installed Eclipse IDE.

https://www.youtube.com/watch?v=35NUuhmQuB4 (Hari this link is for your Reference)

3 Fire fox browser

Selenium by default support Fire fox , so download it


One thing we need to remember here that , Fire fox version should be
WebDriverversion supportive.

4 Firebug Installations:

Open Fire fox Search for Firebug download in google


Click on first link and click on Download Now
Click Install Now
To verify that , right click same page , u can able to see Inspect Element With Firebug
So successfully installed.

5FirePath Installations :

Open Fire fox Search for FirePath download in Google


Click on first link and click on Download Now or Add to Firefox
Click Install Now and click on Restart Now
To verify that , right click same page , u can able to see Inspect Element With Firebug
Click on It
You can able to see FirePath
So successfully installed.

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;

public class Radiobutton {


public static void main(String[] args) throws
InterruptedException {
WebDriver driver=new 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();
}*/

}
}

for(WebElement el:elem){ //for each loop


Thread.sleep(1000);
el.click();
}

Program to automate checkbox 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;

public class checkbox {


public static void main(String[] args) throws

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;

public class Text {


private static final WebElement COURSES = null;
public static void main(String[] args){
WebDriver driver=new FirefoxDriver();
driver.get("https://www.edx.org/");
String
a1=driver.findElement(By.xpath("//a[text()='Courses']")

).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 {

public static void main(String[] args) {


WebDriver driver=new FirefoxDriver();
driver.get("http://www.flipkart.com/");
JavascriptExecutor a1=(JavascriptExecutor)driver;
a1.executeScript("window.scrollBy(0,5000)");
}
}
Program to automate linktext

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;

public class mousemoveover {


public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://www.syndicatebank.in/");
Actions at=new Actions(driver);
WebElement
w2=driver.findElement(By.xpath("//img[@name='about']"));
at.moveToElement(w2).build().perform();
}
}
Program to demonstrate Multiple window
package abc;
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class multiplewindow {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://www.naukri.com/");
driver.findElement(By.xpath("//div[text()='Recruiters']")).click();
Set<String> w1= driver.getWindowHandles();
Iterator<String> w3=w1.iterator();
/*String p1=w3.next();*/
String c1=w3.next();
driver.switchTo().window(c1);
driver.findElement(By.xpath("//button[@id='qsbFormBtn']")).click();

}
}

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 {

public static String readData(String


sheetname, int rownum, int cellnum) {
String rcelldata = null;
try {
FileInputStream fis = new
FileInputStream("C:\\Users\\PRLT05\\Desktop\\reawri1.xlsx");
Workbook wb =
WorkbookFactory.create(fis);
Sheet s = wb.getSheet(sheetname);
Row r = s.getRow(rownum);
Cell c = r.getCell(cellnum);
rcelldata = c.getStringCellValue();
} catch (IOException e) {

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;

public static void writeData(String sheetname,


int rownum, int cellnum, String data){
try {
FileInputStream fis = new
FileInputStream("C:\\Users\\PRLT05\\Desktop\\reawri1.xlsx");

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 {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Excel1 el = new Excel1();
int rn = el.rowNum("Sheet1");
for (int i = 1 ; i <= rn; i++) {
String un = el.readData("Sheet1", i, 0);
String pw = el.readData("Sheet1", i, 1);
driver.navigate().to("https://preview.catalystapps.com/");
driver.findElement(By.name("username")).clear();
driver.findElement(By.name("username")).sendKeys(un);
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys(pw);
driver.findElement(By.id("btn_loginDialog-login")).click();
try{
boolean b1 =
driver.findElement(By.id("createCase")).isDisplayed();
if (b1) {
el.writeData("Sheet1", i, 2, "Pass");
}
else {
el.writeData("Sheet1", i, 2, "Fail");
}
}
catch(ElementNotFoundException e){
e.printStackTrace();
}
}
driver.quit();
}
}
13-7-2015
Synchronization
1.Thread.sleep -> Explicitly wait
2.Implicitly wait
Program to implement Robot class

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;

public class Key {


public static void main(String[] args) throws
InterruptedException, AWTException {
System.setProperty("webdriver.chrome.driver",
"D:\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://mail.google.com/mail/");
Thread.sleep(10000);
driver.findElement(By.id("Email")).sendKeys("quillabc12
34");
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Thread.sleep(10000);
driver.findElement(By.id("Passwd")).sendKeys("quillabc
1234mnbv");
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Thread.sleep(10000);
}
}

Program to demonstrate refresh()

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;

public class Testng1 {


//test case 1
@Test
public void testCase1()
{
System.out.println("In test case 1");
}
@Test
public void testCase2()
{
System.out.println("In test case 2");
}
@BeforeMethod
public void beforeMethod()
{
System.out.println("In before method");
}
@AfterMethod
public void afterMethod()
{
System.out.println("In after method");
}
@BeforeClass
public void beforeClass()

System.out.println("In before class");

}
@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

A framework is often a layered structure indicating what kind of programs can or


should be built and how they would interrelate.

Types of framework
1.Hybrid framework

combination of data driven and key driven framework


2.Data driven framework (Apache POI- Excel)
Reading data from excel
3.Keyword driven framework
It is an independent framework which perform automation based on the
keywords specified in the excel sheet.

Potrebbero piacerti anche