Selenium common face questions and Answers (Java edition)

Source: Internet
Author: User
Tags try catch xpath testng selenium grid

1. How do I tell if an element exists?

Judging whether an element exists or not is different, judging whether it exists means that if the element does not exist at all, it throws nosuchelementexception

This allows you to use try catch and return false if catch to Nosuchelementexception

2. How do I tell if an element appears?

Judging whether the element appears, there are two cases, one is that the element is not at all, nature will not appear, the other is that there is such an element, but it is hidden state

It can be determined by first determining if there is a return false if there is no displayed

3. How to select the elements of the drop-down menu

There are two types of drop-down menus, one for direct use of the SELECT tag, which can be used directly using the selenium API

Reference: http://www.cnblogs.com/tobecrazy/p/4570494.html

Webelement selector = driver.findelement (by.id ("selector"new  Select (selector); Option Select option has the following three methods Selectbyindex (int  index) via Indexselectbyvisibletext (String text) By matching to the visible character Selectbyvalue (String value) by matching the value in the tag

The second drop-down menu is not implemented by select and can be manipulated by JS

Similar to this: http://css3menu.com/(on how to use Devtools please Baidu, do not explain)

So how do you choose a menu like this?

You can finish the demo, the first step of the mouse to "how" use, this time the menu appears; second, click Technical Question

To implement the first step, use the Selenium action Clickandhold, and then you can findbyelement

    Webelement menu = driver.findelement (By.xpath ("//span[.= ' How to use ')");                 New Actions (driver);        Action.clickandhold (menu). Build (). Perform ();         = driver.findelement (by.xpath (                "//ul[@id = ' css3menu_top ']/li[position () =3]/div[@class = ' submenu ']/ div[@class = ' column ']//a[contains (text (), ' Technical ')]);        Technicalquestion.click ();

4. Selenium there are several positioning methods, which one you use most, why?

There are eight ways to locate selenium, and the 3 byname,byclassname,bytagname associated with name

2 Bylinktext,bypartiallinktext related to link

1 Byid related to ID

Two Byxpath and Bycssselector left.

My most common thing byxpath (or cssselector) because in many cases, HTML tag properties are not well-regulated and cannot be located by a single attribute, this time only using XPath can be used to re-implement the positioning of the unique element

In fact the fastest positioning should belong to Byid, because the ID is unique, but most developers do not set the ID

5. Where to go to the Web face test Java implementation
一、 UI自动化测试1、 Qunar机票搜索场景1) 访问Qunar机票首页http://flight.qunar.com,选择“单程”,输入出发、到达城市,选择today+7日后的日期,点“搜索”,跳转到机票单程搜索列表页。2) 在列表页停留1分钟,至到页面上出现“搜索结束”。3) 如果出现航班列表,对于出现“每段航班均需缴纳税费”的行随机点选“订票”按钮,在展开的列表中会出现“第一程”、 “第二程”;对于没有出现“每段航班均需缴纳税费”的行随机点选“订票”按钮,在展开的列表底部中会出现“报价范围”4) 如果不出现航班列表,则页面会出现“该航线当前无可售航班”

Refer to my blog, http://www.cnblogs.com/tobecrazy/p/4752684.html

6. How do I locate dynamically loaded elements on a page?

Trigger dynamic event events, and then findelemnt

If it's a dynamic menu, you need a first-level find

7. How do I locate elements that are dynamically changing the properties?

Property dynamic change means that the element has no fixed attribute value, so it can only be positioned by relative position

For example, through an XPath axis, parent/following-sibling/precent-sibling, etc.

Alternatively, you can try findbyelements traversal

8. How can I improve the efficiency of automated execution of selenium scripts?
    • Optimize test cases to reduce the use of sleep without using implicitlywait
      , while using Selenium's wait/fluentwait, which optimizes the wait time
    • Use selenium grid to implement concurrent execution through testng
    • Operation with JS for some unstable dynamic controls
    • Listener implementation of retry mechanism for overloading testng to improve the success rate of test cases
    • Reduce the use of IE driver,ie execution efficiency is too low!!!
9. What is the principle of webdriver?

Reference: http://www.cnblogs.com/tobecrazy/p/5034408.html

How to ensure the success rate of operating elements in selenium? So how do I ensure that the element I clicked on must be clickable?

Reference: http://www.cnblogs.com/tobecrazy/p/4817946.html

    • Waitforemelentpresent is implemented by encapsulating the Find method, which ensures that elements are found before operations are performed on elements, thus increasing the success rate
    • Before an element is manipulated, such as click, if the element is not display (not hidden), it needs to scroll to the element first and then click

Why use scrolling? Because if the page is not fully displayed, element can only be displayed after a drop-down, it can only be scrolled to the element for click, otherwise the click operation is not possible.

123 JavascriptExecutor js=(JavascriptExecutor)driver;        // roll down and keep the element to the center of browser        js.executeScript("arguments[0].scrollIntoViewIfNeeded(true);", download);

11. What is the PO mode and what is Page factory?

There are many answers on the Internet that are not comprehensive

PO mode is the abbreviation of Page object model, as the name implies, is a design pattern, the implementation of the script page and the real Web page map up, one after the other. This makes testing the framework easier to maintain. For example, a landing page, using the PO mode, will create a LoginPage class, the class will define the user name input box, password input box, login button webelenent

For the corresponding element to implement the corresponding method, the input box is used to input, you need to create a user name and enter the password method, so that the real page consistent, so the design concept is PO mode. The pagefactory belongs to the PO mode, which is used to initialize the page Class of each PO pattern implementation and initialize the object library.

Not to be continued, if you encounter selenium face questions can not answer, please leave a message!

Resources:

Selenium official api:http://seleniumhq.github.io/selenium/docs/api/java/index.html

Where is ethanol going? Interview question: http://www.cnblogs.com/nbkhic/p/3657035.html

Selenium common face questions and Answers (Java edition)

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.