Those pits that were encountered using Webdriver

Source: Internet
Author: User

http://blog.csdn.net/oWuFeng1/article/category/2722111

In the automated end-to-end testing of Web projects, the main use of selenium webdriver is to drive the browser. The advantage of Selenium Webdriver is that it supports many languages and supports many browsers. Mainstream browser chrome, Firefox, IE and other support, mobile phone browser Android, iphone and other support, and even support PHANTOMJS (because PHANTOMJS run test does not need to render elements, so the execution speed).

But I encountered a lot of pits when I was using selenium webdriver. These problems, some because selenium webdriver and browser different version of the compatibility between the problem, some selenium webdriver itself bug, of course, there is no lack of selenium webdriver some of the functions of the understanding is not thorough. I took the time to sum up, share to everyone, I hope you will encounter problems in the future to avoid these pits, less detours. Also summed up some of the use of webdriver better practice, also share to everyone.

    • Webdriver each time an instance of Firefox is launched, an anonymous profile is generated, and the current Firefox profile is not used. This must be noted. For example, if access to the tested Web service needs to pass the proxy, you want to directly set up the Firefox agent is not feasible, because the Webdriver boot Firefox instance does not use your profile, the correct way is set by Firefoxprofile.
123456789         
PublicWebdriverCreate(){ FirefoxprofileFirefoxprofile=NewFirefoxprofile(); Firefoxprofile.Setpreference("Network.proxy.type",1); Firefoxprofile.Setpreference( "network.proxy.http" ,yourproxy firefoxprofile. ( "Network.proxy.http_port" , Yourport firefoxprofile. Setpreference ( "network.proxy.no_proxies_on" , ""  return new firefoxdriver< Span class= "O" > (firefoxprofile }             /span>                

Various other Firefox configurations can also be set via Firefoprofile. If you want to install the plugin by default to Firefox, you can place the plugin in the default plugin folder in the Firefox installation directory, so that you can apply this plugin even with a completely new profile.

    • When using Webdriver to click on the button element on the interface, if the current button element is obscured by other elements on the interface, or does not appear in the interface (such as a button at the bottom of the page, but the screen can only display the top half of the page), use the default Webelement.click () may not trigger the Click event.

The fix is to send a click of the javascript instruction directly after locating the page element.

1
((JavascriptExecutor)webDriver).executeScript("arguments[0].click();", webElement);
    • When a page jump occurs for some operations, it is best to add a wait method to wait for the page load to complete before continuing. The method is to determine whether Document.readystate is complete within a certain time period.
123456789       
    ProtectedFunction<Webdriver,Boolean>ispageloaded(){ ReturnNewFunction<Webdriver,Boolean> (){ @Override PublicBooleanApply(WebdriverDriver){ Return((Javascriptexecutor)Driver).Executescript("Return Document.readystate").equals ( "complete"  }}; } public void waitforpageload () {  webdriverwait wait = new webdriverwait  (webdriver30 wait. (ispageloaded ());                /span>                
    • If the page has AJAX operations, write a wait method to wait for the Ajax operation to complete. The same way as in the previous article. For example, if an AJAX operation is used to populate the DropDownList with data, write a method to determine whether the elements in the DropDownList are 0 extra.
123456789Ten                   +
    PrivateFunction<Webdriver,Boolean>Havemorethanoneoption(FinalByElement){ ReturnNewFunction<Webdriver,Boolean> (){ @Override PublicBooleanApply(WebdriverDriver){ WebelementWebelement=Driver.Findelement(Element); If(Webelement==Null){ ReturnFalse; }Else{ IntSize=Webelement.Findelements(By.TagName("Option")).Size(); ReturnSize>=1}} };} public waitfordropdownlistloaded () { webdriverwait wait = new  Webdriverwait (webdriver30 wait. Until (ispageloaded ());                /span>                

And so on, we can determine whether an element is rendered, whether a class is append or not, and a series of ways to determine whether Ajax is done.

    • If the website uses the jquery animation effect, we can actually run the test disable jquery animation, on the one hand can speed up the test, on the other hand can enhance the stability of the test (if animation is enabled, Some unexpected exceptions may occur when you use Webdriver to drive the browser.
1
((JavascriptExecutor)driver).executeScript("jQuery.fx.off=true");
    • Since Webdriver is going to drive the browser, the test runs for a long time and we can run tests in parallel to save time. If you are using the Maven build tool, you can configure surefire plugin by adding the following configuration on the Configruation node.
123 
<parallel>classes</parallel><threadCount>3</threadCount><perCoreThreadCount>false</perCoreThreadCount>
    • When testing fail, if the currently used Webdriver implements the Takesscreenshot interface, we can call the appropriate method to intercept the Web page rendered by the current browser, which facilitates the quick positioning of the cause of the error.
123456    
    Publicvoidgetscreenshot () { if  (webdriver instanceof takesscreenshot ) { takesscreenshot  Screenshottaker =  (takesscreenshotwebdriver; file file = screenshottaker.getscreenshotas (savepath } }          /span>                
    • If the page pops up the browser's own warning box (using JavaScript's Alert method), Selenium Webdriver will fail sporadically when you click the Warning box. The specific reasons are not yet identified. The solution is to try not to use the Alert method's warning box, but instead implement the modal window (such as the jquery UI's modal window) to implement the warning box effect. This ensures the stability of the test, in addition, we can control the style of the warning box, to bring a better experience for users.

    • Update the version of selenium frequently. Pay attention to the selenium on the official website to see if the new version is released, the new version has fixed those bugs, if it contains the bug you encountered, you can upgrade to the current version.

Those pits that were encountered using Webdriver

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.