Selenium implements automatic acquisition and filling of verification codes when performing automated testing on web pages
The example here is the automated test question java of the 14th Lanqiao Cup Software Testing Competition Simulation Competition
Reference answers for the 14th Lanqiao Cup Software Testing Competition (Simulation Competition/Provincial Competition/National Competition):
https://lanqiao-courses.feishu.cn/docx/DClJdDImXoNMyfxleDMczKdbnYf
1. Example of tested web page
2. Automatically obtain the verification code content
The question requirements are: Enter the verification code (hidden in the tag in the front-end page source code, obtained through the text of the element in the page, and filled in the verification code input box
Right-click the location of the verification code to check and find the span tag where the verification code text is located and the location of the verification code filling box.
3. selenium code
//获取验证码
WebElement verifiCode = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div/div/form/div[3]/div/div/div[2]/span"));
String code_text = verifiCode.getAttribute("textContent");
Thread.sleep(2000);
//输入验证码
WebElement codeInput = driver.findElement(By.xpath("/html/body/div[1]/div/div/div/form/div[3]/div/div[1]/div[1]/input"));
codeInput.sendKeys(code_text);
4. Use assertions to verify user names after logging in—assertEquals()
//登录后,获取登录的账户名称
WebElement accountText = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div/div[1]/div[3]/div[3]/div"));
String result_text = accountText.getText();
Thread.sleep(2000);
// assertEquals(result_text,"超管");
//两种断言形式,使用下述这种Assert时,需要导入import org.junit.Assert;
Assert.assertEquals(result_text, "超管");