Create a new Maven project and add required dependencies
1. Create a new Maven project
2. Add the required dependencies in pom.xml. Here we only need selenium and testng.
<!-- https://mvnrepository.com -->
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.8</version>
<scope>test</scope>
</dependency>
</dependencies>
You can search for the required library in https://mvnrepository.com, select the version, and copy its dependency information
Configure the browser and get a handle
1. Create a new Brower.java, configure browser settings, and create a FirefoxDriver instance
package com.test.testng;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* @Author chunmei deng
* @Description 配置浏览器设置
* @Date 2021/3/21
*/
public class Brower {
WebDriver driver;
public WebDriver firefox(){
String webDriverPath = "/Users/dengchunmei/WebDriver/geckodriver29";
String firefoxPath = "/Applications/Firefox.app/Contents/MacOS/firefox";
//关掉火狐浏览器的日志
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"/dev/null");
System.setProperty("webdriver.gecko.driver",webDriverPath);
System.setProperty("webdriver.firefox.bin",firefoxPath);
FirefoxOptions options = new FirefoxOptions();
options.addArguments("--start-maximized"); // 启动时自动最大化窗口
options.addArguments("--disable-popup-blocking"); // 禁用阻止弹出窗口
options.addArguments("no-sandbox"); // 启动无沙盒模式运行
options.addArguments("disable-extensions"); // 禁用扩展
options.addArguments("no-default-browser-check"); // 默认浏览器检查
Map<String, Object> prefs = new HashMap();
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
//options.setExperimentalOption("prefs", prefs);// 禁用保存密码提示框
driver = new FirefoxDriver(options);
//设置寻找一个元素的时间
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
return driver;
}
}
**Note: For the comparison table between chromdriver and browser versions, please refer to: **https://blog.csdn.net/huilan_same/article/details/51896672
Open the URL, locate the page element and operate
1. Create a new TestDemo.java, mark the initialization method with the @BeforeTest annotation, end the processing with the @AfterTest mark, and the test object method with the @Test mark. Use @Parameters ("searchWord") to specify that the passed parameter values come from the configuration in TestNG.xml
package com.test.testng;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
/**
* @Author chunmei deng
* @Description 测试Demo
* @Date 2021/3/21
*/
public class TestDemo {
WebDriver driver;
@BeforeTest
public void init() {
String url = "http://www.baidu.com";
//新建一个浏览器句柄
driver = new Brower().firefox();
//driver = (WebDriver) new FirefoxDriver();
//打开URL
driver.get(url);
}
@Test
@Parameters("searchWord")
public void search(String searchWord) throws InterruptedException {
//输入搜索字符串
driver.findElement(By.xpath("//*[@id=\"kw\"]")).sendKeys(searchWord);
//点击[百度一下]按钮
driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
Thread.sleep(2000);
}
@AfterTest
public void teardown() {
driver.quit();
}
}
Create a new TestNG.xml and configure the test
1. The content is as follows
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test Of TestNG">
<test verbose="2" name="百度搜索">
<parameter name="searchWord" value=“淘宝" />
<classes>
<class name="com.test.testng.TestDemo">
<methods>
<include name="search" />
</methods>
</class>
</classes>
</test>
</suite>
Run program
1. Run TestNG.xml
2. The running effect is as follows
to generate a test report
1. In the running configuration, check Use default reporters, and the running results will automatically generate a report.