Selenium Webdriver With Java
Selenium Webdriver With Java
Test Automation
Agenda
◎ Slow
◎ Save Time
◎ Repeatability
◎ Reusable
◎ Speed
• Selenium Introduction
• Selenium Features
• Selenium Components
• How Selenium WebDriver Work
1.
Selenium Introduction
Selenium
◎ Open Source
◎ Inbuilt Reporting
3.
Selenium Components
Selenium Components
• Install JAVA
• Install IDE
• Install Selenium
• Configure Selenium Jars
1.
Install JAVA
Install JAVA
◎ Download Java:
◎ https://www.java.com/en/download/
◎ Install Java JDK
2.
Install IDE
Install IDE
◎ Download IDE:
◎ Eclipse Downloads
◎ Open IntelliJ
◎ File > Project Structure
◎ Modules
◎ Mozilla/geckodriver (github.com)
WebDriver BrowserDriver
Write Your First Script Using Selenium WebDriver
◎ Navigate to URL
Write Your First Script Using Selenium WebDriver
◎ Code
Thanks!
Working with Maven
Agenda
• What is Maven ?
• Why Maven ?
• Create Maven Project
1.
What is Maven ?
What is Maven ?
2 4
1
Create Maven Project
◎ Add dependencies
◎ MavenRepository
Thanks!
Selenium Basic Method
Agenda
• get(“url”); • navigate() ;
• getCurrentUrl(); • manage();
• getTitle(); • close();
• getPageSource(); • quit();
• getWindowHandle();
get(“url”);
driver.get("https://www.google.com/");
navigate() ;
driver.manage().window().
getCurrentUrl();
driver.getCurrentUrl();
getTitle();
driver.getTitle();
getPageSource();
driver.getPageSource();
getWindowHandle();
driver.getWindowHandle();
close();
driver.close();
quit();
driver.quit();
Thanks!
Web Elements and Locators
Agenda
• Web Elements
• Web Elements Locator (Inspector)
1.
Web Element
Web Element
◎ Any individual item existed in web page is
called element.
◎ Name
◎ TagName
◎ Class Name
◎ Xpath
◎ CSS
ID Locator
driver.findElement(By.id(“Value"));
“
-Not all elements have IDs
The ID might be auto-generated
Name Locator
driver.findElement(By.name(“Value"));
“
Some elements might have neither ID nor
Name
In this case, we use other selectors
Class Name Locator
driver.findElement(By.Class(“form-control"))
“
“Compound classes are not allowed in
class name locator ”
driver.findElement(By.linkText(“Value"))
Link Text Locator
Link Text Locator
Partial Link Text Locator
driver.findElement(By.partiallinktext(“Value"))
Partial Link Text Locator
Partial Link Text Locator
CSS Locator
◎ CSS using Id
driver.findElement(By.cssSelector(“#id”);
driver.findElement(By.cssSelector(“.parent .child”);
◎ nth-of-type(index)
driver.findElement(By.cssSelector(“.parent .child:nth-of-type(index)”);
XPath Locator
driver.findElement(By.xpath(“//tagName[@attribute = ‘value’]”);
driver.findElement(By.xpath(“//input[@type = ‘search’]”);
◎ Contains
driver.findElement(By.xpath(“//*[contains(@attribute,‘partial value’)]”);
◎ Text
driver.findElement(By.xpath(“//*[text()=‘value’]”);
Thanks!
Interact with browser
Send Keys
◎ Firstly find the element then use SendKeys(“ ”)
driver.findElement(By.xpath(“//*[@attribute,‘value’]”).sendKeys(“ ");
Click
◎ Firstly find the element then use Click(“ ”)
driver.findElement(By.xpath(“//*[@attribute,‘value’]”).click(“ ");
getText
Drop Down
◎ Firstly find the element then use Select Class
WebElement element =driver.findElement(By.cssSelector("[type=submit]"));
exist.
Is Enabled
◎ First find the element then use isEnabled();
driver.findElement(By.cssSelector("[type=submit]")).isEnabled();
◎ To get the Context unique identifier of the all pages you clicked in
set {p1,p2,p3,p…}.
driver.switchTo().window(window);
close();
driver.close();
quit();
driver.quit();
quit();
driver.quit();
Thanks!
Frames
frame();
frame();
driver.switchTo().frame(“name”);
frame();
driver.switchTo().frame(“id”);
frame();
driver.switchTo().frame(“web element”);
parentFrame();
driver.switchTo().parentFrame()
Nested Frames
Main Frame
2
3
Parent
Frame
Child
Frame
Thanks!
Threads & Waits
“
Selenium doesn’t wait if any
element take seconds to be
shown in website.
Thread.sleep();
Thread.sleep(milli seconds);
implicitlyWait();
Is used in all session , and be apply only for element that's take time.
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(seconds))
Disadvantages of implicit Wait
new WebDriverWait(driver,Duration.ofSeconds(10)).until(ExpectedConditions.)
“
Do not mix implicit and explicit waits.
Doing so can cause unpredictable wait
times
Thanks!
Alerts
alert();
driver.findElement(By.id(“id”))
driver.switchTo().alert().accept();
Get text from alert();
driver.switchTo().alert().getText();
Send keys to alert();
driver.switchTo().alert().sendKeys(“Value”);
Thanks!
Testing Framework
(TestNG)
Core functions of a Test Framework
159
1.
What is TestNG
160
What is TestNG
161
TestNG Functionality
◎ Annotations.
◎ Run your tests in arbitrarily big thread pools (in their own
thread)
◎ Flexible test configuration.
◎ Powerful execution model (no more TestSuite).
◎ Supported by a variety of tools and plug-ins (Eclipse, IDEA,
Maven, etc...).
162
Why TestNG
163
2.
Why TestNG
164
Why TestNG
165
3.
Adding TestNG to POM.xml
166
Adding TestNG to POM.xml
◎ 1-Go to https://mvnrepository.com
◎ 2-Go to the latest version stable
◎ 3-Add dependency in pom.xml file
167
4.
TestNG Annotations
168
TestNG Annotations
169
Pre-conditions
◎ @BeforeTest: run before any test method belonging to the classes inside the tag is run.
170
Post-conditions
◎ @AfterClass: run after all the test methods in the current class have been run.
◎ @AfterTest: run after all the test methods belonging to the classes inside the tag
have run.
171
Execution Order
1. Before Suite
2. BeforeTest
3. BeforeClass
4. BeforeMethod
5. Test
6. AfterMethod
7. AfterClass
8. AfterTest
9. AfterSuite
172
5.
TestNG Assertions
173
TestNG Assertions
174
TestNG Assertions
◎ assertTrue: Verifies whether the defined condition is true or not. If true, it will pass
Assert.assertTrue(condition, Message);
assertTrue.
Assert.assertFalse(condition, Message);
175
TestNG Assertions
◎ assertEquals: Compares the expected value with actual value If both are the
same, it passes the test case. If not, it fails the test case.
177
Assertion Types
178
Hard Assert
179
Soft Assert
◎ If assert condition gets failed in Soft Assert :
◎ If there is any exception and you want to throw it then you need to use assertAll()
◎ TestNG will carry out with the next test case of the suite.
180
7.
TestNG Priority
181
TestNG Priority
182
TestNG Priority
◎ Priority in TestNG test cases is a parameter with attribute value
○ E.X: @Test(priority=1)
183
TestNG Priority Notes
◎ Definition of Priority in TestNG test methods can only be the @Test methods.
◎ TestNG will execute @Test annotation with the highest priority (priority = 1) to the
lowest one
184