Here are the steps to validate the message "Password cannot be empty":1. Launch the OrangeHRM application URL2. Enter a valid username 3. Keep the password field empty4. Click on the login button5. Validate that the message "Password cannot be empty" is displayedThe code will be:import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;public class ValidatePasswordMessage { public static void main(String args) { WebDriver driver = new FirefoxDriver(); driver.get("OrangeHRM URL"); driver.find
In this quality assurance training session, you will learn Selenium WebDriver. Topics covered in this course are:
• Selenium Components
• Introduction to Web Driver
• Downloading and Configuring Web Driver with Eclipse
• Web Driver Methods
• Web Driver Locators
• Interacting with different UI elements
• Synchronization, Alert and multiple window
• Dynamic Menus
• Cookie Management
• Launching different web browsers
• Introduction to Test NG
TO know more, visit this link: https://www.mindsmapped.com/courses/quality-assurance/get-practical-training-on-software-testing-quality-assurance-qa/
Ähnlich wie Here are the steps to validate the message "Password cannot be empty":1. Launch the OrangeHRM application URL2. Enter a valid username 3. Keep the password field empty4. Click on the login button5. Validate that the message "Password cannot be empty" is displayedThe code will be:import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;public class ValidatePasswordMessage { public static void main(String args) { WebDriver driver = new FirefoxDriver(); driver.get("OrangeHRM URL"); driver.find
Ähnlich wie Here are the steps to validate the message "Password cannot be empty":1. Launch the OrangeHRM application URL2. Enter a valid username 3. Keep the password field empty4. Click on the login button5. Validate that the message "Password cannot be empty" is displayedThe code will be:import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;public class ValidatePasswordMessage { public static void main(String args) { WebDriver driver = new FirefoxDriver(); driver.get("OrangeHRM URL"); driver.find (20)
[2024]Digital Global Overview Report 2024 Meltwater.pdf
Here are the steps to validate the message "Password cannot be empty":1. Launch the OrangeHRM application URL2. Enter a valid username 3. Keep the password field empty4. Click on the login button5. Validate that the message "Password cannot be empty" is displayedThe code will be:import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;public class ValidatePasswordMessage { public static void main(String args) { WebDriver driver = new FirefoxDriver(); driver.get("OrangeHRM URL"); driver.find
2. Page 2Classification: Restricted
Agenda
• Selenium Components
• Introduction to Web Driver
• Downloading and Configuring Web Driver with Eclipse
• Web Driver Methods
• Web Driver Locators
• Interacting with different UI elements
• Synchronization, Alert and multiple window
• Dynamic Menus
• Cookie Management
• Launching different web browsers
• Introduction to Test NG
3. Page 3Classification: Restricted
1. Selenium Components
Older version
Supports
multiple
browsers
New version
Supports
multiple
browsers
Record Run
tool with UI.
Works only on
Mozilla
Runs parallel test
cases on multiple
machines and
browsers
4. Page 4Classification: Restricted
How to install AUT(Orange HRM):
• https://www.orangehrm.com/OrangeHRM_Installation
1. Download ampstack for windows
2. Even if its 32 or 64 bit machine use the download win32 itself
3. 2. https://bitnami.com/stack/orangehrm
4. click on local install
5. bitnami-orangehrm-3.3.2-3-windows
5. Page 5Classification: Restricted
2. Introduction to Web Driver
• Web Automation framework that allows you to execute tests against
multiple browser
• Main interface for testing
• Creates robust, browser based regression automation suites and tests
• WebDriver has a compact Object Oriented API
• WebDriver overcomes the limitation of Selenium RC
• Provides language support like – Java, C#, Python, Perl, PHP, Ruby
6. Page 6Classification: Restricted
Selenium IDE Selenium RC Selenium WebDriver
Browser: Firefox Browser: IE, Chrome,
Firefox etc
Browser: IE, Chrome,
Firefox et
Record & Playback No record & playback No record & playback
Server start not needed Server start needed Server start not needed
GUI plug in Java Program Core API
Simple to use Easy & small API Complex & large API
Not object oriented Less Object Oriented Entirely Object
Oriented
Doesn’’t support
moving of mouse cursor
Doesn’’t support
moving of mouse cursor
Supports moving of
mouse cursor
7. Page 7Classification: Restricted
3. Pre – Requisites to Download
• Java
• Browsers
• IDE – Eclipse or others
• Jar files
For downloading and configuring Selenium Web driver
Refer to the doc
10. Page 10Classification: Restricted
Introduction to Firebug
• The most popular and powerful web development tool
• Inspect HTML and modify style and layout in real time
• Use the most advanced JavaScript debugger available for any browser
• Get the information you need to get it done with firebug
• Download and install firebug from Add-ons in Mozilla Firefox
11. Page 11Classification: Restricted
Introduction to Firepath
• Firepath is a firebug extension that adds a development tool to edit, inspect
and generate Xpath.
• Download and install fire path
12. Page 12Classification: Restricted
Challenge 1: Validating message “Invalid Credentials”
• Test Case Steps:
1. Launch the URL for AUT in Firefox
2. Type “admin” in Username text box
3. Type “admin” in Password text box
4. Click on “login” button
• Expected Result
-- Application should display message as “Invalid Credentials”
13. Page 13Classification: Restricted
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Challenge1 {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://127.0.0.1:8080/orangehrm/symfony/web/index.php/aut
h/login");
driver.findElement(By.id("txtUsername")).sendKeys(("admin"));
driver.findElement(By.id("txtPassword")).sendKeys(("admin"));
driver.findElement(By.id("btnLogin")).click();
}
}
14. Page 14Classification: Restricted
Challenge 2: Validating message “Invalid Credentials”
• Assignment
--Create new java class : LocateById
-- Modify the above script to click on LOGIN button and fetch the error
message and display the same on console
-- Maximize the browser window before visiting the URL
-- Close the browser window after displaying error message on console
--HINT – use ID as locator for all web elements
15. Page 15Classification: Restricted
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class LocateById{
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://127.0.0.1:8080/orangehrm/symfony/web/index.php/aut
h/login");
driver.findElement(By.id("txtUsername")).sendKeys(("admin"));
driver.findElement(By.id("txtPassword")).sendKeys(("admin"));
driver.findElement(By.id("btnLogin")).click();
System.out.println(driver.findElement(By.id("spanMessage")).getText());
driver.close();
}
}
16. Page 16Classification: Restricted
Challenge 3: Locating By Name
• Assignment :
-- Create new java class: LocateByName
-- Create code for Invalid Login Test Case using Name as Locator for all
applicable WebElements
HINT – use Name as locator for all web elements
17. Page 17Classification: Restricted
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class LocateByName {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://127.0.0.1:8080/orangehrm/symfony/web/index.php/aut
h/login");
driver.findElement(By.name("txtUsername")).sendKeys("admin");
driver.findElement(By.name("txtPassword")).sendKeys("admin");
driver.findElement(By.name("Submit")).click();
System.out.println(driver.findElement(By.id("spanMessage")));
driver.close();
}
18. Page 18Classification: Restricted
Challenge 4: Clicking on a link on a web page
• Test case Steps:
1. Login to Orange HRM
2. Click on Directory link
• Expected Result
-- Directory panel should get displayed
• HINT – use LinkText as locator for clicking link
19. Page 19Classification: Restricted
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class LocateByLinkText {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://127.0.0.1:8080/orangehrm/symfony/web/index.php/auth/login")
;
driver.findElement(By.id("txtUsername")).sendKeys(("admin"));
driver.findElement(By.id("txtPassword")).sendKeys(("seedadmin"));
driver.findElement(By.id("btnLogin")).click();
//driver.findElement(By.id("menu_directory_viewDirectory")).click();
driver.findElement(By.linkText("Directory")).click();
driver.close();
}
}
20. Page 20Classification: Restricted
Challenge 5: Clicking on a link on a web page
• Test case Steps:
1. Login to Orange HRM
2. Click on Directory link
• Expected Result
-- Directory panel should get displayed
• HINT – use PartialLinkText as locator for clicking link
21. Page 21Classification: Restricted
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class LocateByPartialLinkText {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://127.0.0.1:8080/orangehrm/symfony/web/index.php/aut
h/login");
driver.findElement(By.id("txtUsername")).sendKeys(("admin"));
driver.findElement(By.id("txtPassword")).sendKeys(("seedadmin"));
driver.findElement(By.id("btnLogin")).click();
driver.findElement(By.partialLinkText("Dir")).click();
driver.close();
}
}
22. Page 22Classification: Restricted
Challenge 6: Validating Message “Username cannot be empty”
• Test Case steps
1. Launch Orange HRM
2. Click on Login button
• Expected Result
-- Application should display message as “Username cannot be empty”
23. Page 23Classification: Restricted
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class LocateByClass {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://127.0.0.1:8080/orangehrm/symfony/web/index.php/aut
h/login");
driver.findElement(By.className("button")).click();
String msg= driver.findElement(By.id("spanMessage")).getText();
System.out.println("Message is" + msg);
driver.close();
}
}
24. Page 24Classification: Restricted
Difference between findElement & findElements
• findElement() findElements()
-Find the first web element - Find all the elements
Using the given method within the current page
using the given
mechanism
- Returns Web Element - Returns List
25. Page 25Classification: Restricted
Challenge 7: Displaying default text in Login text boxes
• Test Case steps
1. Launch Orange HRM
• Expected Result
-- Default text in Username text box should be Username
-- Default text in Password text box should be Password
26. Page 26Classification: Restricted
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;
public class LocateByTagName {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://127.0.0.1:8080/orangehrm/symfony/web/index.php/auth/login");
List<WebElement> allspans = driver.findElements(By.tagName("span"));
System.out.println("No: of span tags" +allspans.size());
/* for(int i =0; i < allspans.size(); i++) {
System.out.println("Default text:"+allspans.get(i).getText());
} */
for (WebElement we : allspans){
System.out.println(we.getText());
}
driver.close();
}
}
27. Page 27Classification: Restricted
Challenge 8: Validating message “Password can not be empty
• Test Case steps
1. Launch Orange HRM
2. Type “admin” in the username text box
3. Click on Login button
• Expected Result
-- Application should display message as “Password cannot be empty”
28. Page 28Classification: Restricted
CSS Selector
• Using single attribute
-- syntax: tagname[attribute=‘value’]
• Using multiple attribute
--syntax: tagname[attribute=‘value’] [attribute=‘value’]
• Special symbols
- . (Dot) for class
- Syntax: tagname.class
- # for name
- Syntax: tagname#id
29. Page 29Classification: Restricted
Challenge 9: Validating message “Password can not be empty
• Test Case steps
1. Launch Orange HRM
2. Type “admin” in the username text box
3. Click on Login button
• Expected Result
-- Application should display message as “Password cannot be empty
Hint: use By cssSelector: Single Attribute
30. Page 30Classification: Restricted
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class LocateByCss {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://127.0.0.1:8080/orangehrm/symfony/web/index.php/auth/login");
driver.findElement(By.id("txtUsername")).sendKeys("admin");
driver.findElement(By.cssSelector("input[value='LOGIN']")).click();
String msg = driver.findElement(By.id("spanMessage")).getText();
System.out.println("Message is " +msg);
driver.close();
}
}
31. Page 31Classification: Restricted
Challenge 10: Validating message “Password can not be empty
• Test Case steps
1. Launch Orange HRM
2. Type “admin” in the username text box
3. Click on Login button
• Expected Result
-- Application should display message as “Password cannot be empty
Hint: use By cssSelector: Multiple Attributes
32. Page 32Classification: Restricted
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class LocateByCss {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://127.0.0.1:8080/orangehrm/symfony/web/index.php/auth/login")
;
driver.findElement(By.id("txtUsername")).sendKeys("admin");
driver.findElement(By.cssSelector("input[class=‘button’’][value='LOGIN']")).click();
String msg = driver.findElement(By.id("spanMessage")).getText();
System.out.println("Message is " +msg);
driver.close();
}
}
33. Page 33Classification: Restricted
XPath
• What is Xpath?
• The XML path language , is a query language for selecting nodes from an
XML document
• Types of Xpath:
1. Absolute : Always starts with html
2. Relative : Always starts with //
Syntax: //tagname[@attribute=‘value’]
34. Page 34Classification: Restricted
Challenge 11: Validating message “Welcome Admin”
• Test case Steps:
1. Launch the application
2. Login to Orange HRM application
• Expected Result:
- Application should display message as “Welcome Admin”
- Hint: use Absolute Xpath
35. Page 35Classification: Restricted
Solution: Validating Message: “Welcome Admin” By Xpath Absolute
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class AbsolutePath {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://127.0.0.1:8080/orangehrm/symfony/web/index.php/auth/login")
;//absolute xpath
driver.findElement(By.xpath("html/body/div/div/div/form/div[2]/input")).sendKeys(
"admin");
driver.findElement(By.xpath("html/body/div/div/div/form/div[3]/input")).sendKeys(
"seedadmin");
driver.findElement(By.xpath("html/body/div/div/div/form/div[5]/input")).click();
System.out.println(driver.findElement(By.xpath("html/body/div/div/a[2]")).getText(
));
}
}
36. Page 36Classification: Restricted
Challenge 12: Validating message “Welcome Admin”
• Test case Steps:
1. Launch the application
2. Login to Orange HRM application
• Expected Result:
- Application should display message as “Welcome Admin”
- Hint: use Relative Xpath
38. Page 38Classification: Restricted
Web Element Methods
• isDisplayed() : is this element displayed or not? This method avoids the
problem of having to parse an elements “style” attribute.
• isEnabled() : is the element currently enabled or not? This will generally
return true for everything but disabled input elements
• isSelected(): determine whether or not this element is selected or not
• All the above methods returns boolean
39. Page 39Classification: Restricted
Challenge 13: Add Employee with Create Login details
• Test Case steps:
1. Launch the application
2. Login to Orange HRM application
3. Click on PIM Add Employee
4. Select create login details option
• Expected result:
Create login details option should get selected
40. Page 40Classification: Restricted
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class InteractWithChkBox {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://127.0.0.1:8080/orangehrm/symfony/web/index.php/auth/login");
driver.findElement(By.xpath("//input[@id='txtUsername']")).sendKeys("admin");
driver.findElement(By.xpath("//input[@id='txtPassword']")).sendKeys("seedadmin");
driver.findElement(By.xpath("//input[@id='btnLogin']")).click();
driver.findElement(By.linkText("PIM")).click();
driver.findElement(By.linkText("Add Employee")).click();
WebElement cb = driver.findElement(By.id("chkLogin"));
//if there is a chkbox with this id and if its displayed and enabled but not selected then click
if (cb.isDisplayed()){
if(cb.isEnabled()){
if(cb.isSelected()==false) {
cb.click();
}}
}driver.close();
41. Page 41Classification: Restricted
Challenge 14: Employee’s Gender
• Test Case steps:
1. Launch the application
2. Login to Orange HRM application
3. Click on PIM Add Employee
4. Click on any employee
• Expected result:
- Under personal details Gender should be present as Male and Female
- Both options should be disabled before clicking on Edit button
- Both options should become enabled after clicking on Edit button
42. Page 42Classification: Restricted
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class InteractWithRadio {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://127.0.0.1:8080/orangehrm/symfony/web/index.php/auth/login");
driver.findElement(By.xpath("//input[@id='txtUsername']")).sendKeys("admin");
driver.findElement(By.xpath("//input[@id='txtPassword']")).sendKeys("seedadmin");
driver.findElement(By.xpath("//input[@id='btnLogin']")).click();
driver.findElement(By.linkText("PIM")).click();
driver.findElement(By.linkText("Employee List")).click();
driver.findElement(By.linkText("Charu Anil")).click();
//construct the absolute path from the code
}
43. Page 43Classification: Restricted
List<WebElement> genderLabel = driver.findElements(By.xpath("//ul[@class='radio_list']/li/label"));
for(WebElement we:genderLabel){
System.out.println(we.getText());
}
List<WebElement> gender= driver.findElements(By.xpath("//ul[@class='radio_list']/li/input"));
System.out.println("Status before clicking on Edit Button");
for(WebElement we:gender){
System.out.println(we.isEnabled());
}
driver.findElement(By.id("btnSave")).click();
System.out.println("Status after clicking on Save Button");
for(WebElement we:gender) {
System.out.println(we.isEnabled());
}
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.partialLinkText("Welcome")).click();
driver.findElement(By.linkText("Logout")).click();
driver.close();
}
44. Page 44Classification: Restricted
Challenge 15: Country List
• Test Case steps
1. Launch the application
2. Login to orange HRM
3. Click on PIMEmployee list
4. Click on any Employee
5. Click on edit button
6. Select indian as a nationality
• Expected Result
- Default Selected option should be –Select—
- The Nationality drop down should have 100 countries listed
- India should get selected as a nationality
45. Page 45Classification: Restricted
Select Class
• Models SELECT tag providing helper methods to select and deselect options
• Parameterized Constructor
• Methods:
- getFirstSelectedOption() : returns first selected option in this Select tag
- selectByIndex(): Select the option at given index
- selectByValue(): Select all options that have a value matching the argument
- selectByVisibleText(): Select all options that display test matching the
argument
- getOptions(): returns all options belonging to this select tag
46. Page 46Classification: Restricted
import java.util.List;
import java.util.concurrent.TimeUnit;
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;
public class InteractWithDropDown {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://127.0.0.1:8080/orangehrm/symfony/web/index.php/auth/login");
driver.findElement(By.xpath("//input[@id='txtUsername']")).sendKeys("admin");
driver.findElement(By.xpath("//input[@id='txtPassword']")).sendKeys("seedadmin");
driver.findElement(By.xpath("//input[@id='btnLogin']")).click();
driver.findElement(By.linkText("PIM")).click();
driver.findElement(By.linkText("Employee List")).click();
driver.findElement(By.linkText("Charu Anil")).click();
//click on the edit button first to activate it
driver.findElement(By.id("btnSave")).click();
}
47. Page 47Classification: Restricted
WebElement cmbNation = driver.findElement(By.id("personal_cmbNation"));
Select nationality = new Select(cmbNation);
System.out.println("Default Selection:" + nationality.getFirstSelectedOption().getText());
//Get all count of nationality
List<WebElement> nationalities = nationality.getOptions();
System.out.println("Total no: of Nationalities:" + nationalities.size());
//List all nationality
int i = 1;
for (WebElement we: nationalities) {
System.out.println(i+"."+we.getText());
i++;
}//Select India as Nationality by 3 methods
//select byindex
/*nationality.selectByIndex(82);
System.out.println("New Selection Made by index method:"+nationality.getFirstSelectedOption().getText()); */
//select byvisible text
/*nationality.selectByVisibleText("Indian");
System.out.println("New Selection Made by visible text method:"+nationality.getFirstSelectedOption().getText());
*/
nationality.selectByValue("82");
System.out.println("New Selection Made by value method:" +nationality.getFirstSelectedOption().getText());
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.partialLinkText("Welcome")).click();
driver.findElement(By.linkText("Logout")).click();
driver.close();
}
48. Page 48Classification: Restricted
Challenge 16: Handling Table
• Test Case Steps
1. Launch the application
2. Login to Orange HRM application
3. Click on PIM Employee list
• Expected Result
- In Employee table eight columns should be present
- Last seven columns should be (Id, first Name , last name, job title,
Employment status, Sub unit, supervisor
- In employee table on single page maximum 10 records should be
present
49. Page 49Classification: Restricted
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;
public class InteractWithTable {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://127.0.0.1:8080/orangehrm/symfony/web/index.php/auth/login");
driver.findElement(By.xpath("//input[@id='txtUsername']")).sendKeys("admin");
driver.findElement(By.xpath("//input[@id='txtPassword']")).sendKeys("seedadmin");
driver.findElement(By.xpath("//input[@id='btnLogin']")).click();
driver.findElement(By.linkText("PIM")).click();
driver.findElement(By.linkText("Employee List")).click();
50. Page 50Classification: Restricted
//Check eight columns are present
List<WebElement> tblcolumn =
driver.findElements(By.xpath(".//*[@id='resultTable']/thead/tr/th"));
System.out.println("Table size:" + tblcolumn.size());
//Check for last seven columns
List<WebElement> lastseven =
driver.findElements(By.xpath(".//*[@class='header']"));
System.out.println("Total column" + lastseven.size());
//Table should have maximum number of 10 records
List<WebElement> records =
driver.findElements(By.xpath(".//*[@id='resultTable']/tbody/tr"));
if (records.size() <10) {
System.out.println("Total records within range");
}else {
System.out.println("Total records are out of range");
}
}
}
51. Page 51Classification: Restricted
Challenge 17: Handling Screen Shots
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class GetScreenShot {
//Taking a Pic of the webpage
public static void main(String[] args) throws IOException {
WebDriver driver = new FirefoxDriver();
driver.get("https://mail.rediff.com/cgi-bin/login.cgi");
TakesScreenshot ts = (TakesScreenshot)driver;
File src = ts.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("E:venki-
backupSeleniumSeed_SelExSeleniumExScreenShotsrediff.png"));
System.out.println("Screen shot taken");
driver.close();
}
}
52. Page 52Classification: Restricted
Challenge 18: Handling Java Script Alerts
• To handle alert window in Selenium Web driver we have predefined
Interface known as Alert .
• 1- accept()- Will click on the ok button when an alert comes.
• 2- dismiss()- Will click on cancel button when an alert comes
Test Case Steps:
Step 1-Open Firefox and open rediff mail website.
Step 2- Enter username and Click on Go button.
Step 3- Capture the Alert text.
Step 4- Click on the OK button.
53. Page 53Classification: Restricted
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://mail.rediff.com/cgi-bin/login.cgi");
driver.manage().window().maximize();
driver.findElement(By.id("login1")).sendKeys("admin");
driver.findElement(By.name("proceed")).click();
//Switch to alert window and capture the text and print
String alertmsg = driver.switchTo().alert().getText();
System.out.println("Alert message displayed is" + alertmsg);
//Click the ok button inside the alert box
driver.switchTo().alert().accept();
//Click the cancel button inside the alert box
//driver.switchTo().alert().dismiss();
driver.close();
}
57. Page 57Classification: Restricted
Challenge 20: Launch IE
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class LaunchingIE {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.ie.driver","C:AdvancedSeleniumSo
ftwaresIEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("http://127.0.0.1:8080/orangehrm/symfony/web/index.p
hp/auth/login");
}
}
58. Page 58Classification: Restricted
Challenge 21: Launch Google Chrome
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class LaunchingChrome {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","C:AdvancedSeleni
umSoftwareschromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("http://127.0.0.1:8080/orangehrm/symfony/web/index.p
hp/auth/login");
}
}
59. Page 59Classification: Restricted
Challenge 22: Create test case using Mozila firefox, IE, Chrome
• Test case Steps
1. Launch the application “google”
2. Using 3 different browsers -- firefox – IE -- chrome
3. Get the page title from all the browsers.
60. Page 60Classification: Restricted
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class LaunchBrowsers_for {
public static WebDriver driver;
public static int browser;
public static String browsername;
public static void main(String[] args) {
for (browser=1; browser<=3; browser++) {
if(browser == 1) {
driver = new FirefoxDriver();
browsername = "Mozila Firefox Browser";
}
else if (browser ==2) {
System.setProperty("webdriver.ie.driver", "E:venki-
backupSeleniumSoftwareIEDriverServer.exe");
driver = new InternetExplorerDriver();
browsername = "Internet Explorer Browser";
}
62. Page 62Classification: Restricted
TestNG Framework
• TestNG is an advance framework designed in a way to leverage the
benefits by both the developers and testers.
• TestNG was created by an acclaimed programmer named as “Cedric Beust”.
• TestNG with WebDriver gives efficient and effective test result format to
generate test reports.
• TestNG has an inbuilt exception handling mechanism which lets the
program to run without terminating unexpectedly.
63. Page 63Classification: Restricted
Features of TestNG
• Support for annotations
• Support for Data Driven Testing using Dataproviders
• Enables user to set execution priorities for the test methods
• Supports threat safe environment when executing multiple threads
• Readily supports integration with various tools and plug-ins like build tools
(Ant, Maven etc.), Integrated Development Environment (Eclipse).
• Facilitates user with effective means of Report Generation using ReportNG
64. Page 64Classification: Restricted
Annotations
• @Test: annotated method is part of test case
• @BeforeTest: annotated method will run before any test method belonging
to classes inside the tag is run
• @AfterTest: annotated method will run after all the test methods
belonging to classes inside the tag is run
• @BeforeMethod: annotated method will run before each test method
• @AfterMethod: annotated method will run after each test method
• @BeforeSuite: annoatated method will run before all tests in this suite
have run
• @AfterSuite: annotated method will run after all tests in this suite have run
65. Page 65Classification: Restricted
Generating Report using TestNG
• TestCase Steps:
1. Launch the applicaton http://newtours.demoaut.com
2. Goto home page verify its page title
3. Close the application
66. Page 66Classification: Restricted
Import org.testng.Assert;
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 TestNG1 {
public String baseurl = http://newtours.demoaut.com;
public WebDriver driver;
@BeforeTest
Public void setBaseurl( ) {
driver = new FirefoxDriver();
driver.get(baseurl);
}
@Test
Public void verifyTitle() {
String expectedtitle = “Welcome Mercury Tours”;
String actualtitle = driver.getTitle();
Assert.assertEquals(actualtitle , expectedtitle);
}
@AfterTest
Public void endSession() {
driver.close();
}
}