UI Automation Test Selenium (1) Common APIs in--selenium

Source: Internet
Author: User
Tags tagname

Directory

1 Working with browsers
1.1 Open a browser with Webdriver
1.2 Maximize browser & Close Browser
1.3 Setting the browser window size
1.4 Opening the test page
1.5 new window for processing browser popup
2 page element positioning
3 How to manipulate page elements
3.1 Webelement related methods
Processing of 3.2 iframe
3.3 Input box (text field or textarea)
3.4 Drop-down selection box (select)
3.5 Single option (Radio Button)
3.6 Multiple options (checkbox)
3.7 Buttons (button)
3.8 Handling Alert
3.9 Uploading files
3.9.1 element tag is the upload method when input
3.9.2 uploading by manipulating the desktop browser window
3.10 Selenium Treatment HTML5
3.10.1 processing Vedio
3.10.2 Handling Canvas
3.11 Forms (Form)
4 other
4.1 Wait for element to load
4.2 Execute JS Script
4.3 Analog keyboard operation

1 Working with browsers

Return
1.1 Open a browser with Webdriver
Copy Code

Open Firefox Browser:
Webdriver Driver = new Firefoxdriver ();
Open IE browser
Webdriver Driver = new Internetexplorerdriver ();
Open Htmlunit Browser
Webdriverdriver = new Htmlunitdriver ();
Open Chrome Browser
Webdriverdriver = new Chromedriver ();

Copy Code
1.2 Maximize browser & Close Browser

Webdriver Driver = new Firefoxdriver ();
Driver.manage (). window (). Maximize ();
Driver.close ();
Driver.quit ();

1.3 Setting the browser window size
View Code
1.4 Opening the test page

Open the test page
Driver.get ("http://www.baidu.com/");
Driver.navigate (). to ("http://www.baidu.com/");
The Navigate method produces 1 navigator objects that encapsulate some navigation-related methods, such as forward and backward.

1.5 new window for processing browser popup
View Code
2 page element positioning

Return

Webdriver provides the following two ways to locate page elements, parameters are by pair, most commonly by.id and by.name lookups.

findElement   定位某个元素,如果没有找到元素会抛出异常:NoSuchElementExceptionfindElements     定位一组元素

For example, you need to locate the following elements:

<input class= "Input_class" type= "text" name= "passwd" id= "Passwd-id"/>

Copy Code

By.id
webelement element = Driver.findelement (By.id ("Passwd-id"));
By.name
webelement element = Driver.findelement (By.name ("passwd"));
By.xpath
webelement element =driver.findelement (By.xpath ("//input[@id = ' Passwd-id ']");
By.classname
webelement element = Driver.findelement (By.classname ("Input_class"));
By.cssselector
webelement element = Driver.findelement (By.cssselector (". Input_class"));
By.linktext
The popular point is the exact query
Webdriver Driver = new Firefoxdriver ();
Driver.get ("http://www.baidu.com/");
webelement element = Driver.findelement (By.linktext ("Encyclopedia"));
By.partiallinktext:
This method is a fuzzy query
Webdriver Driver = new Firefoxdriver ();
Driver.get ("http://www.baidu.com/");
webelement element = Driver.findelement (By.partiallinktext ("Hao"));
By.tagname
Webdriver Driver = new Firefoxdriver ();
Driver.get ("http://www.baidu.com/");
String test= driver.findelement (by.tagname ("form")). getattribute ("name");
SYSTEM.OUT.PRINTLN (test);

Copy Code
3 How to manipulate page elements

Return
3.1 Webelement related methods
Method Summary
void Clear () If This element was a text entry element, this would clear the value.
void Click () Click this element.
Webelement findelement (by) Find the first webelement using the given method.
Java.util.list<webelement> findelement (by) Find all elements within the current context using the given mechanism .
Java.lang.String getattribute (java.lang.String name) Get The value of a the given attribute of the element.
Java.lang.String Getcssvalue (java.lang.String.propertyName) Get The value of a given CSS property.
Point GetLocation () Where on the page is the top left-hand corner of the rendered element?
Dimension GetSize () What is the width and height of the rendered element?
Java.lang.String Gettagname () Get the tag name of this element.
Java.lang.String GetText () Get the visible (i.e. not hidden by CSS) InnerText of this element, including
Sub-elements, without any leading or trailing whitespace.
Boolean isdisplayed () is this element displayed or not? This method avoids the problem of have to parse an element ' s "style" attribute.
Boolean isenabled () is the element currently enabled or not? This would generally return true for everything but disabled input elements.
Boolean isSelected () determine whether or not this element is selected or not.
void SendKeys (Java.lang.CharSequence ... keystosend) Use the This method to simulate typing into a element, which may set its Value.
void Submit () If This current element was a form, or an element within a form and then this would be submitted to the remote SE RVer.
Processing of 3.2 iframe

Driver.switchto (). FRAME ("City_set_ifr"); The ID of the IFRAME is passed in
Dr.switchto (). Defaultcontent (); If you want to return to the previous default content

3.3 Input box (text field or textarea)

webelement element = Driver.findelement (By.id ("Passwd-id"));
Element.sendkeys ("test");//Enter the contents in the Input box:
Element.clear (); Empty the input box
Element.gettext (); Get the text content of the input box:

3.4 Drop-down selection box (select)
Copy Code

Select select = New Select (Driver.findelement (By.id ("select"));
Select.selectbyvisibletext ("A");
Select.selectbyvalue ("1");
Select.deselectall ();
Select.deselectbyvalue ("1");
Select.deselectbyvisibletext ("A");
Select.getallselectedoptions ();
Select.getfirstselectedoption ();

Copy Code

Example:
View Code
3.5 Single option (Radio Button)

Webelement radio=driver.findelement (by.id ("Bookmode"));
Radio.click (); Select a single option
Radio.clear (); Clear a single option
Radio.isselected (); Determine if a single option has been selected

3.6 Multiple options (checkbox)

webelement checkbox = Driver.findelement (By.id ("MyCheckBox."));
Checkbox.click ();
Checkbox.clear ();
Checkbox.isselected ();
Checkbox.isenabled ();

3.7 Buttons (button)

Webelement btn= driver.findelement (by.id ("Save"));
Btn.click (); Click the button
Btn.isenabled (); Determines whether the button is enable

3.8 Handling Alert

Popup dialog box (popup dialogs)

Alert alert = Driver.switchto (). alert ();
Alert.accept (); Are you sure
Alert.dismiss (); Cancel
Alert.gettext (); Get text

Example:
View Code
3.9 Uploading files
3.9.1 element tag is the upload method when input

The contents of the upload.html file are as follows:

<body>
<input type= "File" id= "Filecontrol" value= "Select Files"/>
</body>

The code is as follows:
View Code
3.9.2 uploading by manipulating the desktop browser window

Example 2 uploading a file
3.10 Selenium Treatment HTML5
3.10.1 processing Vedio

This needs to understand the vedio of HTML5 in the relevant methods, you can refer to http://www.w3school.com.cn/tags/html_ref_audio_video_dom.asp
View Code
3.10.2 Handling Canvas
View Code
3.11 Forms (Form)

The actions of elements in a form, like other element operations, can be submitted to the form after the element operation is complete:
webelement approve = driver.findelement (By.id ("approve"));
Approve.click ();
Or
Approve.submit ();//only suitable for form submission

4 other

Return
4.1 Wait for element to load

Timeout settings

Webdriver Driver = new Firefoxdriver ();
Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds); Time-out when element is recognized
Driver.manage (). Timeouts (). Pageloadtimeout (Timeunit.seconds); Time-out at page load
Driver.manage (). Timeouts (). Setscripttimeout (Timeunit.seconds); Time-out for asynchronous scripts

硬性等待  Thread.sleep(int sleeptime);智能等待设置等待页面加载完毕

View Code
4.2 Execute JS Script

Selenium Common JS Summary

Sometimes we need JS script to assist us in testing, such as we use JS assignment or use JS to perform click Operations. The implementation of the JS script comparison for certain elements is not easy to use, such as web content is too long, the current window is too long, want to click on those not in the current window can see elements can use this method.
View Code
4.3 Analog keyboard operation

Sometimes some elements are inconvenient to click or do other operations, this time can be provided by the selenium of the actions class, it can simulate the mouse and keyboard some of the operation, such as right mouse button, left button, move the mouse and other operations. For these operations, use the Perform () method for execution.
Copy Code

private static void actionsTest(WebDriver driver)        throws InterruptedException {    // 设置等待页面完全加载的时间是10秒,如果在10秒内加载完毕,剩余时间不在等待    driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);    driver.get("https://www.baidu.com/");    By inputBox = By.id("kw");    By searchButton = By.id("su");    // 智能等待元素加载出来    intelligentWait(driver, 10, inputBox);    // 智能等待元素加载出来    intelligentWait(driver, 10, searchButton);    // 实例化action对象    Actions action = new Actions(driver);    // 通过action模拟键盘输入java关键字到 输入框,只有使用了perform方法才会输入进去    action.sendKeys(driver.findElement(searchButton), "java").perform();    // 鼠标模拟移动到搜索按钮    action.moveToElement(driver.findElement(searchButton)).perform();    // 模拟点击操作    action.click().perform();    Thread.sleep(2000);}

UI Automation Test Selenium (1) Common APIs in--selenium

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.