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 org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class LoginTest {
    public static void main(String[] args) throws Exception {
        //Set up chromedriver Address
        System.setProperty("webdriver.chrome.driver", "E:\\selenium\\chromedriver.exe");
        //Create a default browser
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();//window maximizing
        String url = "";//Write address of test
        driver.get(url);
        Thread.sleep(1000);
        //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
        driver.findElement(By.ByCssSelector.cssSelector("input[type=\"text\"]")).clear();
        driver.findElement(By.ByCssSelector.cssSelector("input[type=\"text\"]")).sendKeys("15211111111");
        Thread.sleep(1000);
        //Password   
        driver.findElement(By.ByCssSelector.cssSelector("input[type=\"password\"]")).clear();
        driver.findElement(By.ByCssSelector.cssSelector("input[type=\"password\"]")).sendKeys("111111");
        Thread.sleep(1000);
        //Jump page to use click,submit You can only submit if you want to use submit,And I have to write it down click
        driver.findElement(By.ByCssSelector.cssSelector("button[type='button']")).click();
        Thread.sleep(1000);
        System.out.println("Login successfully,Jump to home");
        //Setting user information, locating elements
        //Full name
        driver.findElement(By.ByXPath.xpath("//div[contains(@class,\"is-required\")][1]/div/div/input")).clear();
        driver.findElement(By.ByXPath.xpath("//div[contains(@class,\"is-required\")][1]/div/div/input")).sendKeys("haha");
        Thread.sleep(1000);
        //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
        driver.findElement(By.ByXPath.xpath("//label[@role=\"radio\"][1]")).click();
        /**
         * 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);
        driver.findElement(By.ByXPath.xpath("//div[contains(@class,'el-date-editor')]/input")).clear();
        driver.findElement(By.ByXPath.xpath("//div[contains(@class,'el-date-editor')]/input")).sendKeys("2020-01-13");
        //Contact number
        Thread.sleep(3000);
        driver.findElement(By.ByXPath.xpath("//div[contains(@class,'is-required')][3]/div/div/input")).clear();
        driver.findElement(By.ByXPath.xpath("//div[contains(@class,'is-required')][3]/div/div/input")).sendKeys("12522222222");
        Thread.sleep(3000);
        /**
         * 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
         */
        driver.findElement(By.ByXPath.xpath("(//div[contains(@class,'el-select')]/div/input)[1]")).click();
        driver.findElement(By.ByXPath.xpath("//*[@class=\"el-select-dropdown__item\"][1]")).click();
        Thread.sleep(3000);
        driver.findElement(By.ByXPath.xpath("(//button[@type='button'])[1]")).click();
        System.out.println("Basic information setting completed");
        driver.quit();//Exit browser

    }
}


Note:

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