Professional Documents
Culture Documents
Webdriver Java 2015
Webdriver Java 2015
Csapó Péter
Mikó Szilárd
1 Assertion
2 Navigation
3 Interrogation
4 Manipulation
CONFIDENTIAL 2
Basics 1
ASSERTION
CONFIDENTIAL 3
Assertion
Fail method
fail(error_message)
Conditional assert
assertTrue(error_message, boolean_condition)
Equality assert
assertEquals(error_message, expected, actual)
CONFIDENTIAL 4
Assertion
Identity assert
assertSame(error_message, expected_object, actual_object)
Custom assert
assertThat(actual_object, Matcher<object> matcher)
String assert
assertThat("myString", containsString("ring"))
CONFIDENTIAL 5
Basics 2
NAVIGATION
CONFIDENTIAL 6
Navigation
• driver.get(java.lang.String)
• driver.navigate().to(java.lang.String)
• driver.navigate().to(java.net.URL)
CONFIDENTIAL 7
Navigation
Refresh page
• Driver.Navigate().Refresh()
CONFIDENTIAL 8
Basics 3
INTERROGATION
CONFIDENTIAL 9
Interrogation
Window Title
• driver.getTitle()
Current URL
• driver.getCurrentUrl()
Page Source
• driver.getPageSource()
CONFIDENTIAL 10
Interrogation
• driver.findElement(org.openqa.selenium.By)
– 2+ matches -> returns only the first match from web page
• driver.findElements(org.openqa.selenium.By)
CONFIDENTIAL 11
Interrogation
By class
• By locating mechanisms
– Id
– ClassName
– LinkText
– PartialLinkText
– Name
– TagName
– CssSelector
– XPath
CONFIDENTIAL 12
Interrogation
• Firefox
• Chrome
• IE
CONFIDENTIAL 13
Interrogation
Id
• driver.findElement(By.id(”some_id”));
ClassName
• driver.findElement(By.className(”some_class_name”));
CONFIDENTIAL 14
Interrogation
Linktext
• driver.findElement(By.linkText(”Sign in”));
• driver.findElement(By.partiallinkText(”Sign”));
Name
• driver.findElement(By.name(”password”));
CONFIDENTIAL 15
Interrogation
tagName
• <label>Email address</label>
• driver.findElement(By.tagName(”label”));
Support classes
CONFIDENTIAL 16
Interrogation
CssSelector
• Absolute path
– driver.findElement(By.cssSelector(”html>body>div>p>input”));
• Relative path
– driver.findElement(By.cssSelector(”input”));
• Attribute selection
– driver.findElement(By.cssSelector(”button[name]”));
– driver.findElement(By.cssSelector(”button[name=‚cancel’]”));
– driver.findElement(By.cssSelector(”img:not[alt]”));
CONFIDENTIAL 17
Interrogation
CssSelector
• Id selection
– driver.findElement(By.cssSelector(”#save”));
• Class selection
– driver.findElement(By.cssSelector(”.login”));
• Combined selection
– driver.findElement(By.cssSelector(”button#save”));
– driver.findElement(By.cssSelector(”input.login”));
CONFIDENTIAL 18
Interrogation
CssSelector
– driver.findElement(By.cssSelector(”div#students:first-child”));
– driver.findElement(By.cssSelector(”#loginForm:nth-child(3)”));
– driver.findElement(By.cssSelector(”button:enabled”));
CONFIDENTIAL 19
Interrogation
XPath
• Absolute path
– driver.findElement(By.xpath(”html/body/p/input”));
• Relative path
– driver.findElement(By.xpath(”//input”));
• Attribute selection
– driver.findElement(By.xpath(”//input[@id=’username’]”));
– driver.findElement(By.xpath(”//*[@id=’myId’]”))
CONFIDENTIAL 20
Interrogation
Element interrogation
• element.getText();
• element.getAttribute();
• element.getTagName();
• element.isDisplayed();
• element.isEnabled();
CONFIDENTIAL 21
Basics 4
MANIPULATION
CONFIDENTIAL 22
Manipulation
Click
• element.click()
– Button
– Link
– Checkbox
– Combobox
Submit
• form.submit()
– Form
CONFIDENTIAL 23
Manipulation
Shift + Click
• Actions(driver).keyDown(Keys.SHIFT).click(element).
keyUp(Keys.SHIFT).build().perform();
Special Actions
• Actions(driver).moveToElement(element).build().perform();
• Actions(driver).contextClick().build().perform();
• Actions(driver).doubleClick().build().perform();
• Actions(driver).clickAndHold().build().perform();
• Actions(driver).release().build().perform();
CONFIDENTIAL 24
Manipulation
Type text
• element.sendKeys(”string”)
– Input field
Clear text
• element.clear()
CONFIDENTIAL 25
Manipulation
Listbox Selection
• new Select(element).selectByIndex(elementCount)
• deselectAll()
CONFIDENTIAL 26
Questions
CONFIDENTIAL 27
Advanced Agenda
1 Synchronization
2 Window Handling
3 Screenshots
4 Browser Profile
5 Cookies
CONFIDENTIAL 28
Advanced 1
SYNCHRONIZATION
CONFIDENTIAL 29
Synchronization
Example
CONFIDENTIAL 30
Synchronization
Implicit Wait
• 0 by default
Example
• driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
CONFIDENTIAL 31
Synchronization
Explicit Wait
• Poor alternative
– Thread.sleep(1000);
• Recommended
– WebDriverWait class
Example
• wait.until(ExpectedConditions.method);
CONFIDENTIAL 32
Synchronization
ExpectedConditions class
• presenceOfElementLocated(By locator)
• titleContains(java.lang.String title)
• visibilityOf(WebElement element)
• invisibilityOfElementLocated(By locator)
• elementToBeSelected(WebElement element)
• elementToBeClickable(By locator)
CONFIDENTIAL 33
Advanced 2
WINDOW HANDLING
CONFIDENTIAL 34
Window Handling
Size
.getHeight();
• driver.manage().window().getSize()
.getWidth();
• driver.manage().window().setSize(Dimension d);
• driver.manage().window().maximize();
Position
.getX();
• driver.manage().window().getPosition()
.getY();
• driver.manage().window().setPosition(Point p);
CONFIDENTIAL 35
Window Handling
Handles
Switch To
• driver.switchTo().window(windowHandle);
CONFIDENTIAL 36
Advanced 3
SCREENSHOTS
CONFIDENTIAL 37
Screenshots
Advantages
Example
• File screenshot =
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
CONFIDENTIAL 38
Advanced 4
BROWSER PROFILE
CONFIDENTIAL 39
Browser Profile
Introduction
• c:\Users\[user]\AppData\Roaming\Mozilla\Firefox\Profiles\
– Passwords
– Bookmarks
– Browser history
– Settings
– Etc.
CONFIDENTIAL 40
Browser Profile
Usages
CONFIDENTIAL 41
Browser Profile
• profile.setPreference("intl.accept_languages", "de");
CONFIDENTIAL 42
Browser Profile
Safari/531.21.10");
CONFIDENTIAL 43
Browser Profile
Enable Extension
• profile.addExtension(new File(PATH_TO_FIREBUG));
• profile.setPreference("extensions.firebug.currentVersion", "2.0.12");
CONFIDENTIAL 44
Advanced 5
COOKIES
CONFIDENTIAL 45
Cookies
Introduction
• Cookie attributes
– Name
– Value
– Domain
– Path
– Expiry
– Secure
– Http only
CONFIDENTIAL 46
Cookies
Interrogation
– driver.manage().getCookies();
– driver.manage().getCookieNamed(cookieToTest);
CONFIDENTIAL 47
Cookies
Manipulation
– driver.manage().deleteAllCookies()
– driver.manage().deleteCookie(TestCookie);
– driver.manage().deleteCookieNamed(cookieToTest);
CONFIDENTIAL 48
Cookies
Manipulation
– driver.manage().addCookie(cookie);
CONFIDENTIAL 49
Questions
CONFIDENTIAL 50
Test Design Agenda
CONFIDENTIAL 51
Test Design 1
CONFIDENTIAL 52
Data Driven Testing
Concept
CONFIDENTIAL 53
Data Driven Testing
• Etc.
CONFIDENTIAL 54
Data Driven Testing
Where to use
CONFIDENTIAL 55
Data Driven Testing
How to use
– @RunWith(JUnitParamsRunner.class)
– @Parameters(method = "testData")
CONFIDENTIAL 56
Test Design 2
CONFIDENTIAL 57
Page Object Model
Agenda
• New Approach
• @FindBy annotation
• PageFactory class
• Page Flow
• Best practices
CONFIDENTIAL 58
Page Object Model
New Approach
CONFIDENTIAL 59
Page Object Model
How to do it
• We accept that the application and its elements are bound to change
CONFIDENTIAL 60
Page Object Model
Rules
CONFIDENTIAL 61
Page Object Model
@FindBy Annotation
2. @FindBy(id = "i")
3. public WebElement i;
CONFIDENTIAL 62
Page Object Model
PageFactory Class
Example
CONFIDENTIAL 63
Page Object Model
Page Flow
CONFIDENTIAL 64
Page Object Model
Best practices
CONFIDENTIAL 65
Questions
CONFIDENTIAL 66
Cucumber agenda
1 Understanding Cucumber
2 Syntax
3 Best practices
CONFIDENTIAL 67
Cucumber 1
UNDERSTANDING
CUCUMBER
CONFIDENTIAL 68
BDD Quick Recap
CONFIDENTIAL 69
Used technologies
CONFIDENTIAL 70
Pros and Cons
Advantages
Disadvantages
CONFIDENTIAL 71
Cucumber 2
SYNTAX
CONFIDENTIAL 72
Feature Files
Feature: A feature would describe the current test script which has to be executed.
Scenario: Scenario describes the steps and expected outcome for a particular test case.
Scenario Outline: Same scenario can be executed for multiple sets of data using scenario
outline. The data is provided by a tabular structure separated by (I I).
CONFIDENTIAL 73
Step Definitions
CONFIDENTIAL 74
Cucumber in Eclipse
CONFIDENTIAL 75
Cucumber in Eclipse
CONFIDENTIAL 76
Basic scenario
@basic
Scenario: Create a search between two cities
Given I open Elvira page
When I create a search from
"Székesfehérvár" to "Sopron"
And I submit the search from
Then the search result title should
contain "Székesfehérvár" and "Sopron"
CONFIDENTIAL 77
Tables scenario
CONFIDENTIAL 78
Advanced tables scenario
CONFIDENTIAL 79
Cucumber 3
BEST PRACTICES
CONFIDENTIAL 80
Cucumber tips 1
CONFIDENTIAL 81
Cucumber tips 2
CONFIDENTIAL 82
Cucumber tips 3
CONFIDENTIAL 83
Cucumber tips 4
CONFIDENTIAL 84
Cucumber tips 5
CONFIDENTIAL 85
Questions
CONFIDENTIAL 86