Selenium3 automated testing in java

First, the environment

This installation jdk1.8

This installation eclipse

Adding eclipse maven assembly

New maven project and add the following fields in POM.xml in:

<dependency>

      <groupId>org.seleniumhq.selenium</groupId>

      <artifactId>selenium-java</artifactId>

      <version>3.14.0</version>

    </dependency>

    <dependency>

      <groupId>org.seleniumhq.selenium</groupId>

      <artifactId>selenium-server</artifactId>

      <version>3.14.0</version>

    </dependency>

1

2

3

4

5

6

7

8

9

10

Second, start the chrome browser

Start chrome browser needs chromedriver, before use it down down to this driver, and then start the browser code;


1, get chromeDriver

chromeDriver obtain address http://chromedriver.storage.googleapis.com/index.html


In this path can be found in all versions of the driver, LATEST_RELEASE open files in this page to see the latest version of the driver.

Open notes.txt into the various versions of the next driver to see this match versions of chrome browser version range, depending on the version of your browser, type of computer system to download the corresponding driver.


As used herein, the windows system as an illustration;


2, start chrome

ChromeDriver understood from the class source code, Chrome instance is created by the fact ChromeDriverService, the source code is as follows:


public ChromeDriver() {

    this(ChromeDriverService.createDefaultService(), new ChromeOptions());

  }

1

2

3

1

2

3

See ChromeDriverService driver name found in the code needs to be set as follows:


/**

 System property that defines the location of the chromedriver executable that will be used by

 the {@link #createDefaultService() default service}.

   */

  public static final String CHROME_DRIVER_EXE_PROPERTY = "webdriver.chrome.driver";

1

2

3

4

5

1

2

3

4

5

Namely webdriver.chrome.driver;


You must add code before create instances chrome

System.setProperty("webdriver.chrome.driver",driverPath);


(1) Examples of simple chrome start as follows:

public static void main( String[] args )

    {

     System.setProperty("webdriver.chrome.driver","D:\\test\\driver\\chromeDriver.exe");

        WebDriver driver = new ChromeDriver();

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

    }

1

2

3

4

5

6

1

2

3

4

5

6

As a result it can start the browser;


(2) chrome custom boot of belt ChromeOptions

Simple code is as follows:


public static void main( String[] args )

    {

     System.setProperty("webdriver.chrome.driver","D:\\test\\driver\\chromeDriver.exe");

     ChromeOptions options = new ChromeOptions();

     WebDriver driver = new ChromeDriver(options);

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

    }

1

2

3

4

5

6

7

1

2

3

4

5

6

7

Which ChromeOptions for adding some common parameters when chrome starts, such as:

options.addArguments("--test-type");

It supports the following functions;

1, add the boot parameters, i.e., use the addArguments ();

2, binary chrome set position, using the method SetBinary ();

3, add extensions, use addExtensions (paths) or

addEncodedExtensions(encoded);

4, adding the agent, a method using discharge setProxy (proxy);

5, is provided to add chrome, use:

options.setExperimentalOption("prefs", prefs);;


The following introduced one of these functions;


1, add the boot argument

As for some of the commonly used chrome startup parameters:


    -user-data-dir = "[PATH]" User Data specified user folder path, the user can bookmark data thus stored in the partition other than the system partition. 

    -disk-cache-dir = "[PATH]" path specified cache Cache 

    -disk-cache-size = Cache specified size in Byte 

    -first run reset to the initial state, the first run 

    -incognito stealth mode is activated 

    –disable-javascript 禁用Javascript 

    -omnibox-popup-count = "num" number of the address bar will prompt the pop-up menu instead of num. 

    -user-agent = "xxxxxxxx" Agent string modified HTTP request header, can be about: See page version Modifying Effects 

    -disable-plugins from loading all plug-ins, you can increase the speed. It can be about: plugins page View results 

    -disable-javascript disabled JavaScript, if you feel slow in this together 

    -disable-java java disabled 

    -start-maximized to maximize boot 

    -no-sandbox sandbox mode canceled 

    -single-process single process run 

    -process-per-tab Each tag uses a separate process 

    -process-per-site using a separate process for each site 

    -in-process-plugins plug-in does not enable separate process 

    -disable-popup-blocking disable popup interception 

    -disable-plugins disable the plug 

    -disable-images disabled image 

    -incognito boot into stealth mode 

    -enable-udd-profiles enable the menu to switch accounts 

    -proxy-pac-url using proxy pac [via 1/2] 

    -lang = zh-CN set the language to Simplified Chinese 

    -disk-cache-dir custom cache directory 

    -disk-cache-size custom cache maximum value (in byte) 

    -media-cache-size Custom Display maximum cache (in byte) 

    -bookmark-menu to add a bookmark button in the toolbar 

    -enable-sync to enable bookmark sync 

    -single-process single process running Google Chrome 

    -start-maximized to maximize launch Google Chrome 

    -disable-java Java ban 

    -no-sandbox non-sandbox mode

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

And examples of common startup parameters:

1, launch a browser to Chinese utf-8 encoding format:


ChromeOptions options = new ChromeOptions();

options.addArguments("--lang=zh_CN.UTF-8");

WebDriver driver = new ChromeDriver(options);

1

2

3

1

2

3

2, the analog iphone mobile phone:


ChromeOptions options = new ChromeOptions();

options.addArguments("user-agent=\"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1\"");

WebDriver driver = new ChromeDriver(options);

1

2

3

1

2

3

3, using the user's browser settings


   ChromeOptions options = new ChromeOptions();

   options.addArguments("--user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data");

   WebDriver driver = new ChromeDriver(options);

1

2

3

1

2

3

Wherein user_name native user name, since the test chrome installed in the default position, so that the address code path is shown; this parameter can not be used in some settings in the configuration code Sometimes we do use chrome browser automation, so You need to use a custom browser configuration settings to start;

Note that this time things start running chrome browser need to turn off the existing chrome browser can run, or will be error, otherwise it will launch a new browser;


4, set your browser to maximize:


ChromeOptions options = new ChromeOptions();

options.addArguments("--start-maximized");  

WebDriver driver = new ChromeDriver(options);

1

2

3

1

2

3

In fact, no where necessary, set to maximize directly from your code is not recommended;


5, ChromeDriver Chrome browser is set to ignore certificate errors alarm:


ChromeOptions options = new ChromeOptions();

options.addArguments("--test-type", "--ignore-certificate-errors");  

WebDriver driver = new ChromeDriver(options);

1

2

3

1

2

3

Herein may be provided in code, it is not recommended;


6, do not open the GUI mode starts chrome

This feature is required for the chrome version, MAC and linux in claim 59 above, and the windows in claim 60 above, and chromeDriver version 2.30+;


ChromeOptions chromeOptions = new ChromeOptions();

chromeOptions.addArguments("--headless");

WebDriver driver = new ChromeDriver(chromeOptions);

1

2

3

1

2

3

In this mode, the time is running you will not see the newly opened browser, but the code can be run properly;


2. Set chrome binary file location

Here chrome.exe for setting the position for chrome browser is not installed in the default position using the following code:


ChromeOptions options = new ChromeOptions();

options.setBinary("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");

WebDriver driver = new ChromeDriver(options);

1

2

3

1

2

3

or


ChromeOptions options = new ChromeOptions();

options.setBinary(new File("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"));

WebDriver driver = new ChromeDriver(options);

1

2

3

1

2

3

3, add the extension application

As shown in the following code, the incoming path extension is a list, or a File object, or multiple objects File extensions path thereof;


ChromeOptions options = new ChromeOptions();

options.addExtensions(new File("/path/to/extension.crx"));

WebDriver driver = new ChromeDriver(options);

1

2

3

1

2

3

4. Add Agent

This section for use on the need to add a proxy to open certain websites machines;

code show as below:


ChromeOptions options = new ChromeOptions();

String proxyIpAndPort = "localhost: 8080"; // proxy address

Proxy proxy = new Proxy();

proxy.setAutodetect (true); // automatically detect proxy settings;

proxy.setFtpProxy (proxyIpAndPort); // ftp proxy address

proxy.setHttpProxy (proxyIpAndPort); // http proxy address

proxy.setNoProxy (proxyIpAndPort); // agentless list of URLs, separated by;

proxy.setProxyAutoconfigUrl (proxyAutoconfigUrl); // address agent automatic installation

proxy.setProxyType (ProxyType.MANUAL); // manual proxy, Required

proxy.setSocksPassword (password); // password

proxy.setSocksUsername (username); // username

proxy.setSocksVersion (5); // add this to automatically set proxyType ProxyType.MANUAL, this is set to release the contents of socks, 4 or 5, the latest 5

proxy.setSslProxy(proxyIpAndPort);

options.setProxy(proxy);

WebDriver driver = new ChromeDriver(options);

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

(Prior to this work useful, but difficult to use, has not been studied End)


5, added chrome settings

To add some chrome chrome set at startup, using as an example:


ChromeOptions options = new ChromeOptions();

Map<String, Object> prefs = new HashMap<String, Object>();

// set a reminder setting, 2 block

prefs.put("profile.default_content_setting_values.notifications", 2);

// set up automatic downloads, two said they did not check, which you can download multiple files without asking

prefs.put("profile.default_content_setting_values.automatic_downloads", 2);

options.setExperimentalOption("prefs", prefs);

WebDriver driver = new ChromeDriver(options);

1

2

3

4

5

6

7

8

1

2

3

4

5

6

7

8

As described above, the contents are provided two setting, this portion corresponds to a setting section of chrome, but not the key value and the setting items are associated with the interface on the chrome, chrome found in the setting items and their corresponding key value and follows :

To set automatic download multiple files without asking, for example:

First, first in chrome set to go check out this checked by default, as follows:


Then enter the settings directory chrome browser, as follows:


Open the Preferences folder, you can be found on the inside of the corresponding settings here need to find themselves a name in accordance follows is just modified after setting a new section (if this is the default settings file is not displayed) :


Therefore, addition of the key code value

profile.default_content_setting_values.automatic_downloads

value is 2 to;


(3) chrome custom boot of belt Capability

Here's an example:


System.setProperty("webdriver.chrome.driver","D:\\test\\driver\\chromeDriver.exe");

ChromeOptions options = new ChromeOptions();

DesiredCapabilities capability = DesiredCapabilities.chrome();

capability.setCapability(ChromeOptions.CAPABILITY, options);

WebDriver driver = new ChromeDriver(capability);

1

2

3

4

5

1

2

3

4

5

As indicated above, the added ChromeOptions can be directly added to the capability, so if there are other needs capability set, this method is recommended to start the browser chrome.

chrome browser settings can also be added by this method, such as:


Map<String, Object> contentSettings = new HashMap<String, Object>();

contentSettings.put("images", 2);

 

Map<String, Object> preferences = new HashMap<String, Object>();

preferences.put("profile.default_content_settings", contentSettings);

 

DesiredCapabilities caps = DesiredCapabilities.chrome();

caps.setCapability("chrome.prefs", preferences);

WebDriver driver = new ChromeDriver(caps);

1

2

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9

For this Proxy may also be added directly in the code as follows:


Guess you like

Origin blog.51cto.com/14529380/2435610