Software Testing Selenium
Software Testing Selenium
-Selenium
LIU Weihong
School of computer science and technology
Anhui university of technology
56381319@qq.com
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
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.
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 ”));
driver.switchTo().frame(fr);
Execute JavaScript script
For example, scroll the scroll bar down to the middl
e position.