Automated testing - selenium launch browser

In the process of automated testing, when you start the browser through selenium, you may need to load plug-ins (such as firebug for testing, or a plug-in must be added in the product), read user data (your own browser configuration file / browser configuration file directly given by others), and set the browser (do not load pictures, etc.).

Since the browser page we launched through selenium is a completely clean page, we need to set it ourselves if we want to bring the information we need.

Here are various ways to start Firefox and Chrome browsers:

1: Firefox

1. Start the browser and use all user data saved on the browser.

The user data is read from the Firefox configuration file. First look at the configuration file of your computer, enter the Firefox installation path on cmd, and run Firefox Exe - profileManger command will pop up all configuration files of Firefox browser on your computer. See the figure below.

//Create profileIni object
        ProfilesIni ini = new ProfilesIni();
        //Get the corresponding configuration file by name, as shown in the figure above, or write "profile2"
        FirefoxProfile profile = ini.getProfile("default");
        //Create a browser driver and pass in the profile. When you start, you will read our default configuration file to call the corresponding Firefox browser
        WebDriver driver = new FirefoxDriver(profile);

        driver.get("https://www.cnblogs.com/");

If it is a configuration file transmitted by others, you save it under the address XXX. (even if you are reading your own local browser configuration file, you can use the following method, just write the path correctly)

//Create file object
        File file = new File("C:\\your\\path\\yourProfileName");
        FirefoxProfile profile = new FirefoxProfile(file);
        
    
        WebDriver driver = new FirefoxDriver(profile);

        driver.get("https://www.cnblogs.com/");

2. Start the browser and set the proxy

FirefoxProfile profile = new FirefoxProfile();
        String proxyIp="192.168.2.111";
        int proxyPort = 6666;
        //Turn on agent mode
        profile.setPreference("network.proxy.type", 1);
        //Set proxy server IP
        profile.setPreference("network.proxy.http", proxyIp);
        //Set the port of the proxy server
        profile.setPreference("network.proxy.http_port", proxyPort);
        
        WebDriver driver = new FirefoxDriver(profile);
        driver.get("http://www.baidu.com");

If you want to know other settings, you can search in Firefox.


3. Start the browser and install the plug-in

//Set the location of your downloaded plug-ins
        File file = new File("D:\\your\\xxx.xpi\\path");
        //Create a profile object for Firefox browser
        FirefoxProfile profile = new FirefoxProfile(file);
        //Call the addExtension method, which extracts the plug-in name from the file path, and adds the name and the object of new FileExtension to the extensions
        //private Map<String, Extension> extensions = Maps.newHashMap();
        profile.addExtension(file);
        
        WebDriver driver = new FirefoxDriver(profile);
        driver.get("https://www.cnblogs.com/");

2: Chrome

Chrome is relatively simple, and its API is very clear. You can refer to the following: https://sites.google.com/a/chromium.org/chromedriver/capabilities . The examples are very clear.

Here are a few examples.

1. Add startup parameters (the following are common font settings, loading user data and simulating mobile phone mode) - addArguments

ChromeOptions options = new ChromeOptions();
        //Read user parameters, XX represents the current user
        options.addArguments("--user-data-dir=C:\\Users\\XX\\AppData\\Local\\Google\\Chrome\\User Data");
    
        //Set language
        options.addArguments("--lang=zh_CN.UTF-8");
        //Analog phone mode
        options.addArguments("--user-agent=iphone 7");
        
        WebDriver driver = new ChromeDriver(options);
        
        driver.get("http://www.baidu.com");

For setting the mobile phone mode, how to fill in the value corresponding to the – user agent? In this description:

Open the browser, press F12, manually set it to phone mode, and then:


About the startup parameters of Chrome browser, the commonly used parameters in our test are as follows:

– disable images

– start maximized

download.default_directory": download_dir (set download save path)

2. Modify settings - setExperimentalOption

ChromeOptions options = new ChromeOptions();
        Map<String, Object> prefs = new HashMap<String, Object>();
        //Disable loading pictures
        prefs.put("profile.default_content_setting_values.images", 2);
        options.setExperimentalOption("prefs", prefs);
        
        WebDriver driver= new ChromeDriver(options);
        driver.get("http://www.baidu.com");

3. Install plug-ins - addExtensions

/*
         * Install plug-ins
         */
        ChromeOptions options = new ChromeOptions();
        options.addExtensions(new File("you/path/XXX"));
        
        WebDriver driver = new ChromeDriver(options);
        driver.get("http: //www.baidu.com");

4. Set agent - DesiredCapabilities

//Set an empty capability object
        DesiredCapabilities caps = new DesiredCapabilities();
        //Set the IP and port number to proxy
        String proxyIpPort="192.168.2.122:8088";
        //Set an empty protocol object
        Proxy proxy=new Proxy();
        //The setting object supports http, ftp and ssl protocols
        proxy.setHttpProxy(proxyIpPort).setFtpProxy(proxyIpPort).setSslProxy(proxyIpPort);
        
        
        //Set the capability object and set the proxy object as the value
        caps.setCapability(CapabilityType.PROXY, proxy);
        
        WebDriver driver = new ChromeDriver(caps);
        driver.get("https://sites.google.com/a/chromium.org/chromedriver/capabilities");

For Chrome user data, you can view it in the C:\Users \ user name \ AppData\Local\Google\Chrome\User Data\Default\preferences file. You can find everything you want to set.

Keywords: Firefox Selenium

Added by LoneTraveler on Fri, 31 Dec 2021 15:25:26 +0200