Selenium Webdriver Study Summary-advanced Usage-cookie, profile (vii)

Source: Internet
Author: User

QQ Group: 136924235Forum: http://bbs.shareku.comfirst, how to use the cookie code example:Import Org.openqa.selenium.Cookie;Mport Org.openqa.selenium.WebDriver;Import Org.openqa.selenium.firefox.FirefoxDriver;Import Org.testng.annotations.Test;Import Java.util.Set;public class Democookies {@Test public void Cookies () {Webdriver Driver = new Firefoxdriver ();Driver.get ("http://bbs.shareku.com/");Sets a cookie,cookie that exists in the field as a key-value pairCookie cookie = new Cookie ("Ray", "male");Driver.manage (). Addcookie (cookie);Outputs all available from the current siteCookie Set allcookies = Driver.manage (). GetCookies ();for (Cookie loaded:allcookies) {System.out.println (String.Format ("Cookie path:%s \n%s-->%s", Loaded.getpath (), Loaded.getname (), Loaded.getvalue ()));}Three ways to delete cookiesDelete the specified cookie by key from the cookieDriver.manage (). deletecookienamed ("Ray");Delete a specified cookie from a cookie objectDriver.manage (). Deletecookie (Allcookies.iterator (). Next ());Delete all Cookies Driver.manage (). Deleteallcookies (); Driver.quit ();}}second, the Firefox user configuration fileAll the settings you make for Firefox, such as your homepage, toolbars, saved passwords, bookmarks, plugin configurations, etc., are saved in a specified user profile folder. Your user profile folder and Firefox programs are separate so that once your Firefox problem occurs, all your information is still safe. That is, you can uninstall Firefox and still keep your settings and other information. User profile details, reference: http://support.mozilla.org/zh-CN/kb/user profile1. Webdriver How to handle profileWhen we initialize Firefox webdriver, we can use an existing profile or a new profile,webdriver to copy it before each use (win7 default storage path C:\users\admini~1\appdata \local\temp\anonymous5354649999399361803webdriver-profile), if you do not specify Firefox profile,webdriver will create an empty profile and use it, Therefore, we do not see the default configuration information for Firefox (for example, missing Firebug components, bookmark information, etc.) in the browser that is Webdriver launched every time.2, Webdriver use the existing Firefoxprofile@Testpublic void Firefoxprofile () {Profilesini allprofiles = new Profilesini ();Firefoxprofile profile = Allprofiles.getprofile ("Webdriver");Webdriver Driver = new Firefoxdriver (profile);Driver.get ("http://bbs.shareku.com/");Driver.quit ();}When the above code starts Firefox, I use my custom profile named "Webdriver", which by default is named "Default".How to customize the profile file:A. Open the DOS console and switch the current directory to the directory where the Firefox.exe file is located.B. Open the user Profile Manager, and enter: firefox.exe-p in the command line.C. In the pop-up dialog box, select "Create Profile", select "Next", then name it, and I'll name it "Webdriver" and then "end".D. Create completed, back to execute the above code, you can find webdriver using our designated profile to start Firefox.By default, the profile installation directory is located in the: C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles directory.3. Use external profile to start Firefox (this profile is not registered in Firefox, such as the profile we copied directly from other machines) .code example:@Testpublic void Unregistedprofile () {File Profiledir = new file ("D:/tmp/webdriver"); Webdriver the profile file that was previously definedFirefoxprofile profile = new Firefoxprofile (profiledir);Webdriver Driver = new Firefoxdriver (profile);Driver.get ("http://bbs.shareku.com/");Driver.quit ();}4. As already mentioned, Firefoxdriver will create an anonymous profile, below to teach you how to customize our anonymous profile a) to start Firefox with the Firebu component@Testpublic void Testfirefoxprofile () {File File = new file ("Src/test/resources/firebug.xpi");Firefoxprofile profile = new Firefoxprofile ();try {Adding Firebug components to profiles created by WebdriverProfile.addextension (file);} catch (IOException e) {E.printstacktrace ();} //Specify the Firefox version, or the Firebug page will open when the Webdriver instance is startedProfile.setpreference ("Extensions.firebug.currentVersion", "1.11.4");Webdriver Driver = new Firefoxdriver (profile);Driver.get ("http://bbs.shareku.com");Driver.quit ();}Execute the above code, you will find that the browser launched with Firebug components, the same method can add more components, if not necessary, we recommend that you minimize the configuration (affecting the Firefox driver boot speed)5. Preferences for browsersThe Setpreference method in Firefoxprofile can change any settings for preferences in the browser, and Firefoxdriver also provides additional configuration (detailed reference: http://code.google.com/p/ Firefoxprofile Settings Part of Selenium/wiki/desiredcapabilities)about the Firefox preferences configuration, you can enter about:config in the browser address bar, carriage return, here can modify the things we care about, where the changes will be applied to the current system, if we just want to start the browser, the Personalization section configuration, Can be done through firefoxprofile. to illustrate:a) Custom Webdriver display specified page at startup Firefoxprofile Pro = new Firefoxprofile ();Pro.setpreference ("Hello", "Webdriver");Pro.setpreference ("Browser.startup.homepage", "http://bbs.shareku.com/");Webdriver Driver = new Firefoxdriver (PRO);Driver.quit ();After the above code executes, the browser will display the Sun Gallery Forum home page when it is launched.b) By default webdriver will wait for the page load to complete after executing the GET, click, and so on, and the script will continue to execute down, which can be provided by Webdriver OfWebdriver.load.strategy"To modify Webdriver ignore this wait. Firefoxprofile Pro = new Firefoxprofile ();Pro.setpreference ("Hello", "Webdriver");Pro.setpreference ("Webdriver.load.strategy"," unstable ");Webdriver Driver = new Firefoxdriver (PRO);This method is used sparingly and can cause many errors that are not intentional.

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.