Understanding of Selenium

Source: Internet
Author: User

Using selenium for automated Web testing is not a day or two. It wasn't until recently that a buddy's blog in TW really understood the development history of selenium. When I started to use it, it was already 2.20.x, And I went directly to the WebDriver. In fact, there are some questions that have been lingering in our hearts, but now we are clear. To sum up.

Reference blog: http://tuohuang.thoughtworkers.org /? P = 157


Selenium RC


Early selenium used javascript injection technology to deal with browsers. Selenium RC was required to start a server and convert API calls for web elements to plain JavaScript, inject this JavaScript code after the selenium kernel starts the browser. Anyone who has developed a web application knows that javascript can obtain and call any elements of the page to perform operations freely. Therefore, the goal of selenium is to automate web operations. The disadvantage of this javascript injection technology is that the speed is not satisfactory, and the stability is greatly dependent on the quality of JavaScript translated by the selenium kernel to APIs.


The start of selenium server and RC are retained so far. The forward compatibility should be considered. The command is as follows:

java -jar selenium-server-standalone-2.14.0.jar -role hubjava -jar selenium-server-standalone-2.14.0.jar -role node  -hub http://localhost:4444/grid/register

WebDriver


After selenium2.x introduces the WebDriver concept, it provides a completely different way to interact with browsers. That is, the browser's native API is encapsulated into a set of more object-oriented selenium WebDriver APIs, directly operating the elements in the browser page, and even operating the browser itself (screenshot, window size, start, disable, install plug-ins, and configure certificates ). Because the browser's native API is used, the speed is greatly improved, and the call stability is handed over to the browser vendor itself, which is obviously more scientific. However, some side effects are that different browser vendors may have some differences in the operation and presentation of web elements, which directly leads to selenium.
WebDriver must be implemented by different browser vendors. For example, Firefox has a dedicated firefoxdriver and chrome has a dedicated chromedriver. (It even includes androiddriver and iOS WebDriver)


Reference an article that I personally agree:If you are using a Webdriver, you can directly discard the selenium server. Because you do not need to start a server to process browser interaction.


A simple example of using WebDriver:

static{System.setProperty("webdriver.firefox.bin","C:/Program Files (x86)/Mozilla Firefox/firefox.exe");}FirefoxDriver driver = new FirefoxDriver();String url = "http://ap13933:8080";driver.manage().window().setSize(new Dimension(1440,1000));driver.get(url);WebElement name = driver.findElement(By.id("UserName"));WebElement pwd = driver.findElement(By.id("OldPassword"));while(!name.isDisplayed() || !pwd.isDisplayed())    sleep(100);        name.clear();pwd.clear();name.sendKeys(username);pwd.sendKeys(password);pwd.submit();

WebDriver experience


The more object-oriented approach of WebDriver greatly reduces the entry threshold of selenium, and the operations on web elements are also very easy to learn. In actual projects, the biggest part of the workload is how you parse and locate various elements on your target project page. For example, if you want to locate a button, you can use ID, CSS, or XPath. To click this button, you have written a function to call the API in selenium, that is, click () or submit () in webelement. What should I do with another button? What if there are hundreds of buttons?


Therefore, you need to have a set of self-implemented algorithms or encapsulation to provide a set of common element locating methods based on the characteristics of the project page. When your general positioning logic can accurately find any element, the rest of the work will be handled in turn and handed over to the selenium webelement API. I think this positioning logic is the biggest part of Web automation workload Using selenium. Of course, some company web projects use self-developed UI frameworks, such as the company where the author is located, so that the positioning rules and algorithms of web elements are easier to design. If the Page code developed by the web project is messy, you need more advanced and rigorous logic to find the elements you want to operate and view!


In my project, I designed and encapsulated a set of common APIs to intelligently locate various types of elements on the page. For example, a large number of dialog and Wizard pages in the project are implemented using Div + CSS. I provide a dialog component with methods such as next (), save (), finish (), click (string buttonname), and cancel (). Then, based on the mask layer and Loading
Icon time to track the progress of the operation. Here is just a small example to share more details.



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.