ctrl+shift+p disable java

1
2
3
4
5
6
7
8
9
10
    	int sleepTime = 1500;
driver.get("https://www.qidian.com");
Thread.sleep(sleepTime);

// 切换窗口
driver.switchTo().window(driver.getWindowHandles().toArray()[1].toString()).click();
Thread.sleep(sleepTime);

// 关闭弹框
driver.switchTo().alert().accept();
1
2
3
//id定位
driver.findElement(By.id("")).click();
Thread.sleep(sleepTime);
1
2
3
//name定位
driver.findElement(By.name("")).click();
Thread.sleep(sleepTime);
1
2
3
//className定位
driver.findElement(By.className("")).click();
Thread.sleep(sleepTime);
1
2
3
//linkText定位
driver.findElement(By.linkText("")).click();
Thread.sleep(sleepTime);
1
2
3
//xpath定位
driver.findElement(By.xpath("")).click();
Thread.sleep(sleepTime);
1
2
3
4
5
// 选择【输入框】,输入【】,并点击【搜索】
driver.findElement(By.className("search_input")).sendKeys("");
Thread.sleep(sleepTime);
driver.findElement(By.className("search_btn")).click();
Thread.sleep(sleepTime);
1
2
3
//tagName定位
driver.findElement(By.xpath("")).click();
Thread.sleep(sleepTime);
1
2
3
4
// 关闭当前页面
driver.close();
// 切换回前一个窗口,其中数字是当前窗口-1
driver.switchTo().window(driver.getWindowHandles().toArray()[1].toString());
1
2
3
4
    	// 模型鼠标悬停
Actions buildr = new Actions(driver);
buildr.moveToElement(driver.findElement(By.xpath("/html/body/div[1]/section/header/div/div/div[2]/div[1]/div[1]"))).perform();
Thread.sleep(1500);
1
2
3
//frame框架定位
driver.switchTo().frame("contentFrame"); // 进入frame // 这个是name属性或者id属性
driver.switchTo().defaultContent(); // 返回默认frame

img

性能测试

修改线程组名称:

image-20231110195919260

添加事务控制器

  • 右击 Thread Group,依次选择 Add - Logic Controller - Transaction Controller,创建 Transaction Controller
  • 创建完成之后,将所有请求或重要请求放入 Transaction Controller

image-20231110200135743

同步定时器

  • 右击重要请求,依次选择 Add - Timer - Synchronizing Timer,创建 Synchronizing Timer
  • 将 Number of Simulated Users to Group by 调整至 1 ~ 10 [10]

image-20231110200312616

image-20231110200405325

参数化请求

右击可以参数化的请求,依次点击 Add - Config Element - CSV Data Set Config

image-20231110200510479

image-20231110200609124

功能测试

浏览器操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 1. 模拟浏览器最大化按钮
driver.manage().window().maximize();

// 2. 设置浏览器宽、高(像素点)
driver.manage().window().setSize(new org.openqa.selenium.Dimension(width, height));

// 3. 设置浏览器位置
driver.manage().window().setPosition(new org.openqa.selenium.Point(x, y));

// 4. 模拟浏览器后退按钮
driver.navigate().back();

// 5. 模拟浏览器前进按钮
driver.navigate().forward();

// 6. 模拟浏览器F5刷新
driver.navigate().refresh();

// 7. 模拟点击浏览器关闭按钮
driver.close();

// 8. 关闭所有程序启动的窗口
driver.quit();

// 9. 获取页面 title
String pageTitle = driver.getTitle();
System.out.println("Page Title: " + pageTitle);

// 10. 获取当前页面 URL
String currentUrl = driver.getCurrentUrl();
System.out.println("Current URL: " + currentUrl);

获取元素信息的常用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 1. 返回元素大小
org.openqa.selenium.Dimension elementSize = element.getSize();
System.out.println("Element Size: " + elementSize);

// 2. 获取元素的文本
String elementText = element.getText();
System.out.println("Element Text: " + elementText);

// 3. 获取属性值,传递的参数为元素的属性名
String attributeValue = element.getAttribute("src");
System.out.println("Attribute Value: " + attributeValue);

// 4. 判断元素是否可见
boolean isDisplayed = element.isDisplayed();
System.out.println("Is Displayed: " + isDisplayed);

// 5. 判断元素是否可用
boolean isEnabled = element.isEnabled();
System.out.println("Is Enabled: " + isEnabled);

// 6. 判断元素是否选中,用来检查复选框或单选按钮是否被选中
boolean isSelected = element.isSelected();
System.out.println("Is Selected: " + isSelected);

鼠标操作的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 定位元素
WebElement element = driver.findElement(By.id("exampleId"));

// 实例化 Actions 对象
Actions actions = new Actions(driver);

// 1. 模拟鼠标右键点击效果
actions.contextClick(element).perform();

// 2. 模拟鼠标双击效果
actions.doubleClick(element).perform();

// 3. 模拟鼠标拖动效果
WebElement sourceElement = driver.findElement(By.id("sourceId"));
WebElement targetElement = driver.findElement(By.id("targetId"));
actions.dragAndDrop(sourceElement, targetElement).perform();

// 4. 模拟鼠标悬停效果
actions.moveToElement(element).perform();

// 5. 执行以上所有鼠标操作
actions.perform();

常用的键盘操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 定位元素
WebElement element = driver.findElement(By.id("exampleId"));

// 1. 删除键(BackSpace)
element.sendKeys(Keys.BACK_SPACE);

// 2. 空格键(Space)
element.sendKeys(Keys.SPACE);

// 3. 制表键(Tab)
element.sendKeys(Keys.TAB);

// 4. 回退键(Esc)
element.sendKeys(Keys.ESCAPE);

// 5. 回车键(Enter)
element.sendKeys(Keys.ENTER);

// 6. 全选(Ctrl+A)
element.sendKeys(Keys.CONTROL, "a");

// 7. 复制(Ctrl+C)
element.sendKeys(Keys.CONTROL, "c");

Select类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 定位<select>元素
WebElement selectElement = driver.findElement(By.id("selectA"));

// 实例化 Select 对象
Select select = new Select(selectElement);

// 1. 根据option索引来定位,从0开始
select.selectByIndex(2);

// 2. 根据option属性 value 值来定位
select.selectByValue("optionValue");

// 3. 根据option显示文本来定位
select.selectByVisibleText("Option Text");

弹框处理

1
2
3
4
5
6
7
8
9
10
11
12
13
// 1. 获取弹出框对象
Alert alert = driver.switchTo().alert();

// 2. 调用操作
// 2.1 返回alert/confirm/prompt中的文字信息
String alertText = alert.getText();
System.out.println("Alert Text: " + alertText);

// 2.2 接受对话框选项
alert.accept();

// 2.3 取消对话框选项
alert.dismiss();

滚动条操作

1
2
3
4
5
6
7
8
9
import org.openqa.selenium.JavascriptExecutor;
// 1. 设置JavaScript脚本控制滚动条
String jsScript = "window.scrollTo(0, 1000);";

// 2. Selenium调用执行JavaScript脚本的方法
((JavascriptExecutor) driver).executeScript(jsScript);


String jsScript = "window.scrollTo(0, document.body.scrollHeight);";

ifame

1
2
3
4
5
6
7
     // 1. 切换到指定 frame 的方法
switchToFrame(driver, "frameReference");
// frame_reference:可以为frame框架的name、id或者定位到的frame元素
// 在切换后的 frame 中进行其他操作,例如查找元素等

// 2. 恢复默认页面方法
switchToDefaultContent(driver);

多窗口切换

1

窗口截图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    // 截图保存的路径
String screenshotPath = "C:/Users/Administrator.SC-201902031211/Desktop/abc.png";

// 调用截图方法
takeScreenshot(driver, screenshotPath);
// 截图方法
public static void takeScreenshot(WebDriver driver, String screenshotPath) {
try {
// 截图
File screenshotFile = ((org.openqa.selenium.TakesScreenshot) driver).getScreenshotAs(org.openqa.selenium.OutputType.FILE);

// 将文件复制到指定路径
BufferedImage image = ImageIO.read(screenshotFile);
ImageIO.write(image, "png", new File(screenshotPath));

System.out.println("截图成功,保存路径:" + screenshotPath);
} catch (IOException e) {
System.out.println("截图失败:" + e.getMessage());
}
}

操作cookie

1
2
3
4
5
6
7
8
9
10
// 1. 获取指定 cookie
Cookie specificCookie = getSpecificCookie(driver, "cookieName");
System.out.println("指定 cookie 值:" + specificCookie);

// 2. 获取所有本地 cookies
Set<Cookie> allCookies = getAllCookies(driver);
System.out.println("所有本地 cookies:" + allCookies);

// 3. 添加 cookie
addCookie(driver, "cookieName", "cookieValue");

配置

账号: 1649743146@qq.com 密码:Dep520.

仓库地址: https://gitee.com/getp/web-test.git

测试用例

第一阶段,测试步骤,预期结果,编写人和执行人填自己,评审员也可以填自己或指导 老师,执行状态未执行 (不需要写实际执行结果)

测试步骤写1-2-3-4

举手找监考老师

第二阶段:执行测试,编写实际执行结果,执行状态要按实际情况来选择(未执行,执行成功,执行失败)

实际结果与预期结果不一致,要填执行失败

得分规则:发现bug。

web项目用例编写:
UI测试:界面元素,缩放,文案(内容,错别字等)–标准:
原型图
链接测试:
功能测试

从上到下:

  1. title icon

image-20231201222704070

image-20231201222546553

image-20231201222824715

image-20231201223140505

image-20231201223221167

image-20231201224343997


http://example.com/2023/12/01/模版/
作者
Deng ErPu
发布于
2023年12月1日
许可协议