0% found this document useful (0 votes)
38 views

Software Testing Selenium

Uploaded by

Tridip Barua
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Software Testing Selenium

Uploaded by

Tridip Barua
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 38

Test Automation

-Selenium
LIU Weihong
School of computer science and technology
Anhui university of technology
[email protected]
Outlines
• Build test environments
• How to use Selenium
Build test environments
Step1:
You need to prepare:
1. Firefox or chrome, recent
versions(download it by yourself)
2. JDK, version 1.8
3. Eclipse, cannot be too old version
4. Webdriver
5. Selenium, version 3.11
Build test environments
Step1.1:
install Firefox or chrome. You can get it
from the internet very easily.
Build test environments
Step1.2:
1. install JDK, version 1.8
2. set the environment variables:
JAVA_HOME, CLASSPATH, PATH
(I installed JDK to disk C:\)
Test if jdk is installed correctly
Build test environments
Step1.3:
install Eclipse.
You can put the folder (eclipse_win64) in
C:\
You can also use the Eclipse you
downloaded from the internet.
Build test environments
Step1.4:
put the two .exe files( in folder “Webdriver”)
in the root directory of your eclipse
Build test environments
Step1.5:
import Selenium, version 3.11
1. new a Java Project, input a project name, such
as “WebTest”
2. Add External Jars….
3. new a package
4. new a class
5. write your code
Example 1:
open a new Firefox browser
load the Baidu homepage
search for “selenium”
Example 2:
open a Firefox browser
load the login page on Xuexitong
Type username and password
Assert whether the expected and
actual results are same
How to use selenium?
Selenium -Page Elements
Elements can be accessed in many ways:
•Element attribute: name, id, value
•Xpath

User actions that can be emulated:


•Click
•Filling out text fields (sending keys)

www.rackspace.com
Selenium – Inspecting Elements
Selenium – How to access page
elements
• There are several different ways the
webdriver can find HTML elements:
driver.findElement(By.id("user")).sendKeys("liuweihong");

driver.findElement(By.name("password")).sendKeys(“aaa
a");

driver.findElement(By.xpath("//
*[@id=\"kw\"]")).sendKeys("selenium");

driver.findElement(By.linkText(" 学术 ")).click();
Start a Broswer:
• Firefox:
System.setProperty(“webdriver.firefox.bin”,”E:\\program
files\\Mozilla firefox\\firefox.exe”);
• IE:
System.setProperty(“webdriver.ie.driver”,”C:\\program files\\
IEDriverServer.exe”);
• Chrome:
System.setProperty(“webdriver.chrome.driver”,”C:\\program
files\\chromedriver.exe”)
The actions for text field or text
area
• Find element :
WebElement element =
driver.findElement(By.id(“passwd-id”));
• Enter characters :
element.sendKeys(“test”);
• delete the existing characters :
element.clear();
• get the characters in the text field or text area :
element.getText();
The actions for Drop-down list
• Find element :
Select select = new
Select(driver.findElement(By.id(“select”)));
Select the particular option:
select.selectByVisibleText(“testName”);
or
select.selectByValue(“name”);
get the selected option :
select.getAllSelectedOptions();
select.getFirstSelectedOption();
The actions for Radio Button
• Find element :
WebElement
sex=driver.findElement(By.id(“sex”));
• Select a radio button :
sex.click();
• Check if a radion button is selected
already :
sex.isSelected();
The actions for checkbox
• WebElement area
=driver.findElement(By.id(“area .”));
• area .click();
• area .clear();
• area .isSelected();
• area .isEnabled();
The actions for button
• WebElement saveButton =
driver.findElement(By.id(“save”));
• saveButton.click();
• saveButton.isEnabled ();
The actions for the left and right selection boxes.

After the left items are selected, the items


will be added to the right select box.
Select name= new
Select(driver.findElement(By.id(“name”)));
name.selectByVisibleText(“hellen”);

WebElement
addName=driver.findElement(By.id(“addButton”));

addName.click();
The actions for Popup dialogs
Alert alert = driver.switchTo().alert();
alert.accept();
alert.dismiss();
alert.getText();
Ps :
(1)The Alert pop-up box is not an HTML page element, but
a JavaScript control; So you cannot right-click to inspect
its source code.
(2) These operations are only suitable for the alert/confirm/
prompt dialogs.If it is a window just looking like a pop-up
dialog, you need to operate it by finding the elemenet.
The actions for Upload File
WebElement picFile =
driver.findElement(By.id(“picFile ”));

String filePath = “d:\\report\\


600x600x0.jpg”;
picFile .sendKeys(filePath);
Multi windows or frames
switching
• Switch back to the outermost HTML page
driver.switchTo().defaultContent();
• Switch to a particular frame :
driver.switchTo().frame(“leftFrame”);
driver.switchTo().frame(“mainFrame”);
Switch to a window :
driver.switchTo().window(“windowName”);
The Navigation
• Open a new page :
driver.navigate().to(“http://www.baidu.com”
);
• Go to the previous or forward page by the
navigation history :
driver.navigate().forward();
driver.navigate().back();
Difficulties in automated functio
nal testing
• Element positioning often leads to situations wher
e it cannot be located. If it cannot be located, ther
e are four reasons to consider:
1. Page loading is too slow. Code execution is very
fast.
Solution: set waiting time
2. Incorrect expression
Solution: Verify that the expression is correct befor
e writing the script, and learn to write xpath by you
rself
Difficulties in automated functio
nal testing
3. More than one page is open
Solution: Switch to a correct window handle
4. Web page built-in framework
Solution: Switch to a correct frame
The method of handwritten xpat
h
1. In the developer tool, use ctrl+f to gradually
verify the correctness of xpath until the desire
d element is found
2. In the console, enter the $x ("xpath") expre
ssion to verify the correctness of xpath and fin
d the only element is the ultimate goal.
Switch to a window handle
• String Handle1=driver.getWindowHandle();
Set<String> allHandles2=driver.getWindowHandles();
String Handle2="";
for (String handle : allHandles2)
{
if (!handle.equals(Handle1))
{
Handle2=handle;
}
}
driver.switchTo().window(Handle2);
Switch to a window handle
Set<String> handles=driver.getWindowHandles();
String targetHandle=null;
for(String handle:handles)
{
if(driver.switchTo().window(handle).getTitle().equals(“ 网易
云音乐” ))
targetHandle=handle;
}
driver.switchTo().window(targetHandle);
exercises
• Login function testing on Xuexitong
• open Xuexitong from the homepage of
Academic Affairs Office--Login--deliver an
announcement--delete the announcement
switch to an iframe
1. Use iframe's id or name, ps:does not support
xpath
driver.switchTo().frame("contentFrame");
2. When the frame does not have an ID or name,
you can first locate the frame element and then
switch to it
WebElement fr=driver.findElement(By.xpath("/
html/body/div[2]/div[2]/iframe"));

driver.switchTo().frame(fr);
Execute JavaScript script
For example, scroll the scroll bar down to the middl
e position.

JavascriptExecutor js = (JavascriptExecutor) driver;


js.executeScript("window.scrollTo(0, document.bod
y.scrollHeight / 2)");
Specify the execution order of th
e testing methods
Page Object Model
Page Object Model is a commonly used design pa
ttern in the development of automated testing proje
cts. Its core idea is to convert a page into an object
in a programming language, and convert the opera
tions of the page into methods of the object. The p
urpose of doing so is to achieve the separation of
page operations and testing logic, thereby improvi
ng the readability and maintainability of the code, r
educing redundant code, and reducing the couplin
g between business code and testing code.

You might also like