在线OJ平台UI自动化测试用例
UI自动化测试
整体的代码框架如图所示
下来拆分具体每个模块的代码
导入依赖`
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<version>1.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
</dependencies>`
初始化浏览器
为了增加代码的可修改性,我们将初始化浏览器写成一个方法,方便后续增加不同驱动
package common;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Init {
public static WebDriver webDriver;
public static WebDriver start(){
//指定驱动位置
System.setProperty("webdriver.chrome.driver", "E:\\driver\\chromedriver-win64\\chromedriver.exe");
webDriver=new ChromeDriver();
return webDriver;
}
}
关闭浏览器
@AfterAll
@Test
static void end(){
webDriver.quit();
}
获取题目列表数量
@Test
void numOfProblemTest(){
//webDriver.get("http://8.130.81.18:8080/Online-OJ/");
//获取首页所有题目的id数量
int num=webDriver.findElements(By.xpath("//*[@id=\"problemTable\"]/tr[1]/td[3]")).size();
System.out.println(num);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//题目数不等于即判定通过
Assertions.assertNotEquals(0,num);
}
判断链接跳转
@Test
void numOfProblemTest(){
//webDriver.get("http://8.130.81.18:8080/Online-OJ/");
//获取首页所有题目的id数量
int num=webDriver.findElements(By.xpath("//*[@id=\"problemTable\"]/tr[1]/td[3]")).size();
System.out.println(num);
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//题目数不等于即判定通过
Assertions.assertNotEquals(0,num);
}
题目跳转
@Test
void jumpProblrem(){
webDriver.findElement(By.xpath("//*[@id=\"problemTable\"]/tr[2]/td[2]/a")).click();
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//获取所有窗口句柄
Set<String> handles=webDriver.getWindowHandles();
//将窗口句柄存储到List中
List<String> handleList=new ArrayList<>(handles);
//切换到新页面的窗口句柄
webDriver.switchTo().window(handleList.get(1));
//获取当前页面url
String cur= webDriver.getCurrentUrl();
//判断
Assertions.assertEquals(PROBLEMSUTE,cur);
}
输入正确代码并运行
@Test
void testRightCode() throws InterruptedException {
//输入测试用例
webDriver.findElement(By.xpath("//*[@id=\"editor\"]/textarea"))
.sendKeys("class Solution {" +
" public int[] twoSum(int[] nums, int target) {" +
" int n = nums.length;" +
" for (int i = 0; i < n; ++i) {" +
" for (int j = i + 1; j < n; ++j) {" +
" if (nums[i] + nums[j] == target) {" +
" return new int[]{i, j};" +
" }" +
" }" +
" }" +
" return new int[0];" +
" }" +
"}");
webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
//点击提交
webDriver.findElement(By.xpath("//*[@id=\"submitButton\"]")).click();
sleep(10000);
//接收结果返回框文本内容
WebElement element=webDriver.findElement(By.xpath("//*[@id=\"problemResult\"]"));
sleep(1000);
String result=element.getText();
//判断结果是否为都通过
Assertions.assertEquals(ALLPASS,result);
}
输入错误代码并运行
@Test
void testCode() throws InterruptedException {
//重新加载
webDriver.get("http://8.130.81.18:8080/Online-OJ/problemDetail.html?id=2");
sleep(1000);
//清空
webDriver.findElement(By.xpath("//*[@id=\"editor\"]/textarea")).clear();
sleep(1000);
//输入测试用例
webDriver.findElement(By.xpath("//*[@id=\"editor\"]/textarea"))
.sendKeys("1");
webDriver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
//点击提交
webDriver.findElement(By.xpath("//*[@id=\"submitButton\"]")).click();
sleep(1000);
//接收结果返回框文本内容
String result=webDriver.findElement(By.xpath("//*[@id=\"problemResult\"]")).getText();
sleep(1000);
//判断结果是否为都通过
Assertions.assertEquals(FALSE,result);
}
UI自动化录制效果
在线OJ平台UI自动化录制
遇到的问题
元素未渲染完全就选定元素
在测试过程中出现了元素未渲染成功就选定元素的情况,使用getText()拿不到文本内容:
解决方法:
每执行一步就等待一下,让页面把元素加载完全再获取内容:
有新窗口出现时,仍获取的是原来窗口的URL
在进行题目跳转进行判断的时候,发现获取当前URL一直是原来的页面的URL:
解决方法:
//获取所有窗口句柄
Set<String> handles=webDriver.getWindowHandles();
//将窗口句柄存储到List中
List<String> handleList=new ArrayList<>(handles);
//切换到新页面的窗口句柄
webDriver.switchTo().window(handleList.get(1));
xpath定位不准
定位元素,我一般都是在开发者工具里直接copy-copy-xpath,但是这次在定位一个元素时一直获取不到它,系统报错:org.openqa.selenium.ElementNotInteractableException: element not interactable (Session info: chrome=123.0.6312.106)
解决方法:
多试,一定要多试,自动定位的xpath不准就只能手动一个一个定位。