0% found this document useful (0 votes)
20 views38 pages

Selenium Test Automation Guide

Uploaded by

Tridip Barua
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views38 pages

Selenium Test Automation Guide

Uploaded by

Tridip Barua
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Test Automation

-Selenium
LIU Weihong
School of computer science and technology
Anhui university of technology
56381319@[Link]
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)

[Link]
Selenium – Inspecting Elements
Selenium – How to access page
elements
• There are several different ways the
webdriver can find HTML elements:
[Link]([Link]("user")).sendKeys("liuweihong");

[Link]([Link]("password")).sendKeys(“aaa
a");

[Link]([Link]("//
*[@id=\"kw\"]")).sendKeys("selenium");

[Link]([Link](" 学术 ")).click();
Start a Broswer:
• Firefox:
[Link](“[Link]”,”E:\\program
files\\Mozilla firefox\\[Link]”);
• IE:
[Link](“[Link]”,”C:\\program files\\
[Link]”);
• Chrome:
[Link](“[Link]”,”C:\\program
files\\[Link]”)
The actions for text field or text
area
• Find element :
WebElement element =
[Link]([Link](“passwd-id”));
• Enter characters :
[Link](“test”);
• delete the existing characters :
[Link]();
• get the characters in the text field or text area :
[Link]();
The actions for Drop-down list
• Find element :
Select select = new
Select([Link]([Link](“select”)));
Select the particular option:
[Link](“testName”);
or
[Link](“name”);
get the selected option :
[Link]();
[Link]();
The actions for Radio Button
• Find element :
WebElement
sex=[Link]([Link](“sex”));
• Select a radio button :
[Link]();
• Check if a radion button is selected
already :
[Link]();
The actions for checkbox
• WebElement area
=[Link]([Link](“area .”));
• area .click();
• area .clear();
• area .isSelected();
• area .isEnabled();
The actions for button
• WebElement saveButton =
[Link]([Link](“save”));
• [Link]();
• [Link] ();
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([Link]([Link](“name”)));
[Link](“hellen”);

WebElement
addName=[Link]([Link](“addButton”));

[Link]();
The actions for Popup dialogs
Alert alert = [Link]().alert();
[Link]();
[Link]();
[Link]();
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 [Link] 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 =
[Link]([Link](“picFile ”));

String filePath = “d:\\report\\


[Link]”;
picFile .sendKeys(filePath);
Multi windows or frames
switching
• Switch back to the outermost HTML page
[Link]().defaultContent();
• Switch to a particular frame :
[Link]().frame(“leftFrame”);
[Link]().frame(“mainFrame”);
Switch to a window :
[Link]().window(“windowName”);
The Navigation
• Open a new page :
[Link]().to(“[Link]
);
• Go to the previous or forward page by the
navigation history :
[Link]().forward();
[Link]().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=[Link]();
Set<String> allHandles2=[Link]();
String Handle2="";
for (String handle : allHandles2)
{
if (![Link](Handle1))
{
Handle2=handle;
}
}
[Link]().window(Handle2);
Switch to a window handle
Set<String> handles=[Link]();
String targetHandle=null;
for(String handle:handles)
{
if([Link]().window(handle).getTitle().equals(“ 网易
云音乐” ))
targetHandle=handle;
}
[Link]().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
[Link]().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=[Link]([Link]("/
html/body/div[2]/div[2]/iframe"));

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

JavascriptExecutor js = (JavascriptExecutor) driver;


[Link]("[Link](0, [Link]
[Link] / 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.

Common questions

Powered by AI

Web elements in Selenium are accessed using methods like driver.findElement() with selectors such as id, name, or Xpath. Manipulation of these elements varies: for buttons, click() can be used; for text fields, sendKeys() and clear() are applicable; for dropdowns, the Select class offers options like selectByValue(). Selenium provides a comprehensive API for interacting with page elements efficiently .

Pop-up dialogs in Selenium testing are handled using the Alert interface, which does not represent HTML elements but JavaScript controls. Selenium provides methods like alert.accept(), alert.dismiss(), and alert.getText() to interact with these dialogs. The challenge is that pop-ups are JavaScript-based and not accessible via standard HTML element inspection tools, requiring different handling than typical HTML elements .

In Selenium, multi-window handling is achieved using methods like driver.switchTo().window() for switching control to a different window using window handles. For multi-frame web applications, driver.switchTo().frame() allows switching to specific frames by name or id, or by locating the frame element directly if no name or id is present .

Selenium facilitates user interactions by providing methods to perform actions on various web page elements. Dropdowns can be controlled with the Select class and methods like selectByVisibleText(). Radio buttons and checkboxes can be selected or checked with WebElement's click() and isSelected() methods. Text fields are interacted using sendKeys() for entering text and clear() for removing the existing text .

The Page Object Model improves the development of automated testing projects by representing a page as an object in a programming language, converting page operations into methods of the object. This approach enhances code readability and maintainability, helps reduce redundant code, and separates page operations from testing logic to decrease the coupling between business and testing code .

Common challenges in automated functional testing with Selenium include difficulty in element positioning due to slow page loading or incorrect expressions, and issues arising from multiple open pages or web page frameworks. Solutions include setting appropriate wait times, verifying expressions, switching to the correct window handles or frames, and manually writing Xpath expressions to ensure precise element targeting .

Switching to a newly opened browser window in Selenium involves obtaining the current window handle with driver.getWindowHandle(), then iterating over all available window handles using driver.getWindowHandles(). By comparing each handle to ensure it's not the current one, Selenium can switch contexts with driver.switchTo().window() for the desired new window .

Setting environment variables such as JAVA_HOME and PATH is crucial for a functioning Selenium test environment. JAVA_HOME points to the JDK installation directory, ensuring the Java compiler and runtime are accessible. PATH includes the Java bin directory for command-line tool execution, which is essential for running Java programs. These configurations ensure that Selenium scripts run smoothly and that Java-based tools are recognized system-wide .

Verifying the correctness of Xpath expressions is crucial in Selenium testing to ensure that the desired elements are accurately located. Correct Xpath expressions improve the reliability and accuracy of automated tests, especially when dealing with dynamic content or multiple similar elements. This can be achieved using developer tools with Ctrl+F to iteratively verify expressions, which is a necessary step to avoid errors in element selection .

Setting up a Selenium test environment requires the installation of a recent version of Firefox or Chrome, JDK version 1.8 with proper environment variables (JAVA_HOME, CLASSPATH, PATH), Eclipse (newer versions), Webdriver, and Selenium version 3.11 .

You might also like