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