环境准备:
1.环境准备:
1)安装java环境,我的java version "1.8.0_241"
2)安装eclipse
3)下载selenium 下载路径:http://selenium-release.storage.googleapis.com/index.html ,
我下载了最新版本 selenium 4.0-beta-3\
http://selenium-release.storage.googleapis.com/index.html?path=4.0-beta-3/
4)下载浏览器驱动,注意浏览器驱动的类型和版本号需要和你计算机上的浏览器版本匹配。
浏览器驱动器和浏览器通讯,向浏览器发出请求,接收响应。
不同浏览器有各自的浏览器驱动程序,以控制浏览器。浏览器驱动程序可以看做是代理。chrome、firefox、IE等浏览器都有自己的浏览器驱动程序。
根据你要自动化测试使用的浏览器及其版本,下载相应的浏览器驱动程序
需要注意的是,浏览器驱动程序需要和浏览器的版本相匹配。
各浏览器驱动下载路径如下图所示。选择浏览器后,点击【download】,在下载页面选择合适的版本即可。
/documentation/getting_started/installing_browser_drivers/ ,该页面中部内容如下:
我使用的chrome浏览器,我的谷歌浏览器版本90.0.4430.93,所以下载的浏览器驱动是chromedriver_win32_v90_0_4430_24.zip,
chrome浏览器驱动下载路径:ChromeDriver Mirror
chrome浏览器版本务必要和驱动版本匹配。对应关系可参考:https://www.cnblogs.com/biyuting/p/8673357.html
建议和我保持一致,以免版本匹配出现问题,节外生枝。
2.第一个selneium自动化测试脚本
1)在eclipse中创建java工程
-
2)将selenium的java依赖包加到工程中
添加后点击【Apply and close]
-
3)工程中添加浏览器驱动文件(在系统环境变量path添加浏览器驱动的路径也行)
创建resouce目录,将浏览器的驱动复制到该文件夹,方便后续使用(该步不做也可以)
-
4)新建类文件 test1.java,编写selenium代码,并运行
代码如下
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Test1 {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","resource\\chromedriver.exe"); //设置chrome驱动程序的路径
System.out.println(System.getProperty("webdriver.chrome.driver"));
WebDriver driver = new ChromeDriver(); //初始化一个chrome驱动实例,保存到driver中
// WebDriver driver = = new FirefoxDriver();
driver.manage().window().maximize(); //最大化窗口
//设置隐性等待时间
driver.get("https://www.runoob.com/html/html-tutorial.html");
Thread.sleep(3000);
System.out.println("当前打开页面的标题是: "+ driver.getTitle());
//关闭并退出浏览器
driver.quit();
}
}
运行结果:
5)将junit框架引入到工程中
引入junit框架的方法:
选中项目,点击右键菜单 “Build path”,将junit库添加到libraries中
尝试写了个更复杂的例子,可能对新同学来说太难了,但是其实认真看看,也还是可以看懂的奥!
由于链接会打开新的页签,要在新的页面上操作的前提是先切换到那个页签,selenium切换稍微有一丢丢麻烦,这个就是套路留下来以后直接用就好了,这个是有点复杂的。
import static org.junit.jupiter.api.Assertions.assertEquals;
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.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import junit.framework.Assert;
public class p2p {
@SuppressWarnings("deprecation")
@Test
void test01() throws InterruptedException {
//设置chrome驱动程序的路径
System.setProperty("webdriver.chrome.driver","resource\\chromedriver.exe");
System.out.println(System.getProperty("webdriver.chrome.driver"));//打印驱动器路径
//初始化一个chrome驱动实例,保存到driver中
WebDriver driver = new ChromeDriver();
//最大化窗口
driver.manage().window().maximize();
//打开指定url的页面
driver.get("https://www.runoob.com/html/html-tutorial.html");
//设置等待3s
Thread.sleep(3000);
//将浏览器窗口句柄保存到originalWindow(以便和后面链接打开的tab页区分)
String originalWindow = driver.getWindowHandle();
//找到页面上的id值为"s"的元素,保存到searchText
WebElement searchText = driver.findElement(By.id("s"));
//向该元素中输入:xpath换行符
searchText.sendKeys("xpath\n");
//循环查找直到找到新窗口
String secondWindow ="" ;
for (String windowHandle : driver.getWindowHandles()) {
if(!originalWindow.contentEquals(windowHandle)) {
driver.switchTo().window(windowHandle);
secondWindow=windowHandle;
break;
}
}
//打印页面标题
System.out.println("第二个打开页面的标题是: "+ driver.getTitle());
//点击链接文字
driver.findElement(By.partialLinkText("xpath")).click();
//循环查找直到找到新窗口
String thirdWindow = "";
for (String windowHandle : driver.getWindowHandles()) {
if(!originalWindow.contentEquals(windowHandle) && !secondWindow.contentEquals(windowHandle) ) {
driver.switchTo().window(windowHandle);
thirdWindow=windowHandle;
break;
}
}
//打印页面标题
System.out.println("当第三个页面的标题是: "+ driver.getTitle());
//使用junit的断言,检查打开页面的url地址是否符合预期
Assert.assertEquals("https://www.runoob.com/xpath/xpath-tutorial.html",driver.getCurrentUrl() );
Thread.sleep(3000);
//关闭并退出浏览器
driver.quit();
}
}