Java uses selenium to implement automated search of Chrome, FireFox and IE with assertions

First create a Maven project

Add in pom.xml, this is the latest Maven dependency package for selenium

<dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.0.0-alpha-5</version>
    </dependency>

Next, download the selenium driver required by the 3 browsers

Insert picture description here
I have downloaded the latest driver here , provided that all browser versions are up to date ; you can click here to download all three if you need them.
Link: https://pan.baidu.com/s/18M1Fya1ZJbEpy0SskI9nqQ extraction code: 0naa
If you do n’t want to download, there is a second method, provided that the browser has this selenium plugin (only Google and Firefox, IE has no plugin) And Google must also have an account to open the Google online store, as shown below:
Insert picture description here

First, Chrome's implementation code

import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Chrome_Browser {
    @Test
    public void f() throws InterruptedException {
        //写代码操作浏览器,模拟人工测试:api
//        System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
//      1、  创建一个Chrome驱动
        WebDriver driver = new ChromeDriver();
//        2、输入百度网站进行访问
        driver.get("http://www.baidu.com");
//      3、找到百度输入框(元素定位,定位一个元素)
//        根据id去找  kw
        WebElement input = driver.findElement(By.id("kw"));
//        说明确实找到这个输入框
        System.out.println(input.getAttribute("maxlength"));
//        4、往输入框输入关键字
        input.sendKeys("CSDN");
//        5、点击百度一下
        WebElement searchButton = driver.findElement(By.id("su"));
        searchButton.click();
        //绝对路径
//        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/form/span[2]/input")).click();
        //相对路径
        driver.findElement(By.xpath("//*[@id=\"su\"]")).click();

//        获得输入框的value值
        String actualkeywords = driver.findElement(By.id("kw")).getAttribute("value");
        System.out.println("输入框的值:" + actualkeywords);
// 		断言: 验证结果符合我的预期,符合就不打印;不符合,就把不同结果打印出来
        Assert.assertEquals("CSDN", actualkeywords);
        //  等待时间 3秒
        Thread.sleep(3000);
        //退出浏览器
        driver.quit();
    }
}

Second, FireFox implementation code

import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FireFox_Browser {
    @Test
    public void f() throws InterruptedException {
        //写代码操作浏览器,模拟人工测试:api
//        System.setProperty("webdriver.firefox.driver", "src/main/resources/geckodriver.exe");
//        1、firefox驱动
        WebDriver driver = new FirefoxDriver();
//        2、输入百度网站进行访问
        driver.get("http://www.baidu.com");
//      3、找到百度输入框(元素定位,定位一个元素)
//        根据id去找  kw
        WebElement input = driver.findElement(By.id("kw"));
//        说明确实找到这个输入框
        System.out.println(input.getAttribute("maxlength"));
//        4、往输入框输入关键字
        input.sendKeys("CSDN");
//        5、点击百度一下
        WebElement searchButton = driver.findElement(By.id("su"));
        searchButton.click();
        //绝对路径
//        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/form/span[2]/input")).click();
        //相对路径
        driver.findElement(By.xpath("//*[@id=\"su\"]")).click();

//        获得输入框的value值
        String actualkeywords = driver.findElement(By.id("kw")).getAttribute("value");
        System.out.println("输入框的值:" + actualkeywords);
////        验证结果符合我的预期,符合就不打印;不符合,就把不同结果打印出来
        Assert.assertEquals("CSDN", actualkeywords);
//        等待时间 3秒
        Thread.sleep(3000);
        //退出浏览器
        driver.quit();
    }
}

Three, IE browser implementation code

This is more troublesome, so it is a little special, there will be a security reminder, it is best to turn off all anti-virus software, otherwise you may be prompted to intercept information.

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

/**
 * @author ZF
 * @Description TODO
 * @Date 2020/4/9 20:29
 */
public class IE_Browser {
    @Test
    public void test() throws InterruptedException {
//        1、驱动文件找不到的异常
        System.setProperty("webdriver.ie.driver", "src/main/resources/IEDriverServer.exe");
//        创建一个对象,用来设置创建ie驱动时的各种设置
        DesiredCapabilities capabilities = new DesiredCapabilities();
//        2、取消IE安全设置(忽略IE的Protected Mode的设置)
        capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
//        3、忽略浏览器的页面缩放设置
        capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
//        4、设置一个初始化页面,防止window对象丢失
        capabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, true);
//        IE驱动
        WebDriver driver = new InternetExplorerDriver(capabilities);
//        2、输入百度网站进行访问
        driver.get("http://www.baidu.com");
//        等待时间 3秒
        Thread.sleep(3000);
        driver.quit();
    }
}

to sum up:

I do n’t have to visit these codes, especially IE. Many blogs say that you need to remove the checkbox for enabling protection mode in the security settings. In fact, there is no need to remove them. Experience: all settings are unnecessary, just turn off 360 antivirus software That's it, to prevent malware from being prompted . The following picture can be opened if checked, and there are no environment variables for all browsers to set, and it is not necessary. Just follow my simplest blog on the entire network, and it can be used. It includes a lot of code and python. suitable. The following picture is the best evidence to prove that those blogs do not need to be set up.
Insert picture description here

Published 79 original articles · 321 praised · 40,000+ views

Guess you like

Origin blog.csdn.net/qq_43107323/article/details/105423492