首页 技术 正文
技术 2022年11月6日
0 收藏 652 点赞 613 浏览 3721 个字

框架搭建

基于maven+jdk8+junit5+seleium 构建

 <dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.6.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-java-commons</artifactId>
<version>2.13.5</version>
<scope>compile</scope>
</dependency>
</dependencies>

驱动下载

下载地址 :http://chromedriver.storage.googleapis.com/index.html

基于当前 浏览器的版本

测试demo

@Test
public void test1() throws IOException {
ChromeDriver driver = new ChromeDriver();
System. setProperty("webdriver.chrome.driver", "path");
//设置全屏
driver.manage().window().maximize();
driver.get("https://home.testing-studio.com/");
//d.findElement(By.cssSelector(". d-button-abl").cick();
driver.findElement(By.xpath("//span[contains(text(),'登录')]")).click();
snapshot((TakesScreenshot)driver, "截图"+FakerUtils.getTimeStamp()+".jpg");
}

截图方法:TakesScreenshot接口是依赖于具体的浏览器API操作的

// 截屏方法
public static void snapshot(TakesScreenshot drivername, String filename) {
String currentPath = System.getProperty("user.dir"); //get current work folder
System.out.println(currentPath);
File scrFile = drivername.getScreenshotAs(OutputType.FILE);
try {
System.out.println("save snapshot path is:"+currentPath+"/"+filename);
FileUtils.copyFile(scrFile, new File(currentPath+"\\"+filename));
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Can't save screenshot");
e.printStackTrace();
} finally {
System.out.println("screen shot finished");
}
}

执行

用列录制

使用seleniumIDE 录制

  1. 新建一个录制project

输入网站开始录制,并在录制的网页上执行搜索

录制结果

当Test越来越多时,可以将多个Test归类到Suites中,Suites就像小柜子

创建项目时,IDE会创建一个默认Suite,并将第一个Test添加到其中,你可以点击Test,在下拉菜单中选中Test suites进入Suites管理界面

首先进入Suites管理界面,点击`+`,提供名称,然后单击add:

将鼠标悬停在`suite1`上,点击三个点弹出Suites管理菜单:

可以对`suite1`进行管理,包括添加test,重命名,删除,设置,导出

导出的用例

用例的编写

public class testCase {
public static WebDriver driver;
@BeforeAll
static void initData(){
//加载驱动
driver=new ChromeDriver();
// 设置全局隐式等待 5S
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
} @Test
void Login(){
//读取测试链接
driver.get("https://home.testing-studio.com/");
//获取登录按钮
driver.findElement(By.xpath("//span[contains(text(),'登录')]")).click();
driver.manage().window().maximize();
//清理记住的原账号与密码
driver.findElement(By.id("login-account-name")).clear();
driver.findElement(By.id("login-account-name")).sendKeys("*"); driver.findElement(By.id("login-account-password")).clear();
driver.findElement(By.id("login-account-password")).sendKeys("*");
driver.findElement(By.id("login-button")).click();
// 截图
FakerUtils.snapshot((TakesScreenshot)driver,"截图"+FakerUtils.getTimeStamp()+".jpg");
} @AfterAll
static void tearDown(){
driver.quit();
}}

定位方法

执行分析:

Driver的初始化,每个测试用例执行都可以通过这个一个方法获得一个driver.get()打开一个网址

find_element(By.定位符,”)

一个页面还没有完全加载完全,点击这个元素,发现这个元素是有问题的,元素找不到或者不可点击,等等,可以强行加sleep(不推荐)

每个元素定位的时候,都会去find_element查找一个元素,在这个时候,通常我们需要引入一个新的机制,这个机制叫做隐式等待,解决元素找不到的问题,在规定的时间内,自动的去等待元素的出现,元素找到了,但是状态不对,不可点击也会报错

等待方式

隐式等待:

设置一个等待时间轮询(默认0.5s)查找 元素是否出现 (服务端)

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

显式等待:

在客户端本地每隔0.5s巡查下条件是否匹配 需要 实例化 webdriverwait

在代码中定义等待条件,当条件发生时才继续执行代码`WebDriverWait`配合until()方法,根据判断条件进行等待

程序每隔一段时间(默认为0.5秒)进行条件判断,如果条件成立,则执行下一步,否则继续等待,直到超过设置的最长时间

webDriverWait= new WebDriverWait(driver,100L);

强制等待

线程等待,线程休眠一段时间,Thread.sleep(2000)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905