About reducing Selenium automation script redundancy improvements

Source: Internet
Author: User

In the earliest contact with selenium, think can write the basic script in Java, can run, such as the following a section of the positioning element script

1 driver.findelement (by.id ("name")). Click (); 2 driver.findelement (By.xpath ("//input[contains (@data-value, ' sort ')]"). Click;
Seeing the above script, we can encapsulate a method separately, using the code to improve the simplicity, so we can customize a method Locatedrivereleso we can improve our original script.
1 this. Locatedriverele (driver, "id", "name"). Click (); 2 this. Locatedriverele (Driver, "XPath", "//input[contains (@data-value, ' sort ')]"). Click;
This script seems to be much simpler, but the next time the developer improves the UI of the system and maintains the script, it can be found that the steps we have to manipulate the UI are unchanged, only the path of the positioned element, so that we can improve on the original basis, using Pagefactory thought, Create 2 classes for the tested interface, the page and test classes, which are specifically for the tested program elements, and the test class as the true testing class
11 @FindBy (xpath= "//input[contains (@data-value, ' sort ')]")22privatewebelement XPath;33privatewebelement ID;44Publicpage () {55 CO =NewCom ();66 pagefactory.initelements (Driver, This);77 }88publicvoid XPath () {99 This. Xpath.click;Ten10 } One11publicvoid ID () { A12 This. Id.click; -13}

So our original script could be changed to

1 page page=new  page (); 2 page.id (); 3 Page.xpath ();

After the change, our script looks a lot simpler, and the development of the UI, we just maintain the path of the elements in the page class, the script looks simple, easy to maintain there is no understanding of the place can give me a message

About reducing Selenium automation script redundancy improvements

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.