Java+Selenium automatic test learning

Basic process of automatic test

1. Set the address System.setProperty() of the chrome driver;

2. Create a default browser, chromedriver = new chromedriver();

3. Set window properties (customization, such as setting window size);

4. Load a new web page driver.get(url) in the browser window;

5. There are two ways to set wait: display wait and implicit wait;

6. Positioning elements;

7. Exit the browser.

package testLogin;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import java.util.concurrent.TimeUnit;

public class LoginTest {
    public static void main(String[] args) throws Exception {
        //Set up chromedriver Address
        System.setProperty("", "E:\\selenium\\chromedriver.exe");
        //Create a default browser
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();//window maximizing
        String url = "";//Write address of test
        //Implicitly wait. The global variable sets the loading time for all elements in the page, waiting for 10 seconds in total. If the element does not exist after 10 seconds, an exception will be thrown
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        //Locate login interface elements
        //If there is a value before the text box, clear the value before the element first
        //Phone number
        //Jump page to use click,submit You can only submit if you want to use submit,And I have to write it down click
        System.out.println("Login successfully,Jump to home");
        //Setting user information, locating elements
        //Full name
        //Gender,The radio box is a click event. Note: this path is not input In the label radio,Generally, it's positioning input Inside radio
         * Birthday
         * Locate date control:
         *  1.There is readonly in the date control. Remove the readonly attribute through js
         *  2.Locate element, enter date manually
         *  3.There is an additional attribute disabled, which needs to be removed
        //adopt js Get attribute information
        String jsScript = "document.getElementsByClassName('el-date-editor')[0].getElementsByTagName('input')[0].removeAttribute('readonly')";
        String jsScript1 = "document.getElementsByClassName('el-date-editor')[0].getElementsByTagName('input')[0].removeAttribute('disabled')";
        //implement js
        ((ChromeDriver) driver).executeScript(jsScript);
        ((ChromeDriver) driver).executeScript(jsScript1);
        //Contact number
         * Drop-down box
         * Note: the drop-down box here is not a select type, but a pull-down box simulated by ul and li
         *       1,When locating the drop-down box, input the box first and click
         *       2,Click in the location drop-down box
        System.out.println("Basic information setting completed");
        driver.quit();//Exit browser



1. If there are double quotes in the double quotes, you can escape them automatically or add single quotes.
1. Single quotation mark: "/ / button [@ type ='button '] [1]"
2. Escape: "/ / div [contains (@ class, \" is required\“

2. Three ways to wait.

3. This is not the location method of the select type drop-down box (view the drop-down box type). Our drop-down box is written by ur\li, for example:

1. click the input box first, and then pop up the drop-down box;
2. Relocate the element click in the drop-down box, and assign the last value to the input box

4. Click and submit:
1. submit form event
2. click to respond to the event.
For example: when I log in, I submit with submit, but I do not jump to the homepage. You can use click.

5. Locate date control: date control usually has readonly or disable attribute, and then enter date in locating element.
1. First remove the attribute document. Getelementsbyclassname ("") [0]. Removeaattribute ('readonly ') with js code
2. Then use the sendkeys() method to enter the date when the element is located

6. There are two ways to exit:
1. close() closes the current window. If it is the last window currently opened, exit the browser
2. quit() closes all associated windows

Tags: Java Attribute Selenium Windows

Posted on Wed, 15 Jan 2020 22:58:55 -0500 by d-woo