Those pits that were encountered using Webdriver

Source: Internet
Author: User

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.

Public Webdriver Create () {  firefoxprofile firefoxprofile = new Firefoxprofile ();  Firefoxprofile.setpreference ("network.proxy.type", 1);  Firefoxprofile.setpreference ("network.proxy.http", yourproxy);  Firefoxprofile.setpreference ("network.proxy.http_port", yourport);  Firefoxprofile.setpreference ("network.proxy.no_proxies_on", "" ");  return new Firefoxdriver (firefoxprofile);}

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 command directly after locating the page Element.

((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.

protected function<webdriver, boolean> ispageloaded () {return new function<webdriver, B oolean> () {@Override public Boolean apply (webdriver driver) {return (Javascript            Executor) driver). executescript ("return document.readystate"). equals ("complete");    }        };        } public void Waitforpageload () {webdriverwait wait = new webdriverwait (webdriver, 30);    Wait.until (ispageloaded ()); 

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.
Private function<webdriver, boolean> havemorethanoneoption (final by Element) {        return new function< webdriver, boolean> () {            @Override public            Boolean apply (webdriver driver) {                webelement webelement = Driver.findelement (element);                if (webelement = = Null) {                    return false;                } else {                    int size = webelement.findelements (by.tagname ("option")). Size ();                    Return size >= 1;}}        ;    }     public void waitfordropdownlistloaded () {        webdriverwait wait = new webdriverwait (webdriver, +);        Wait.until (ispageloaded ());    }

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're running

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.

((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. (netease Dagger Framework supports the Practice)

<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.

public void Getscreenshot () {        if (webdriver instanceof takesscreenshot) {            takesscreenshot screenshottaker = ( Takesscreenshot) webdriver;            File File = Screenshottaker.getscreenshotas (savepath);        }    }

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.