Selenium是在java中编写错误检查的最佳方法?
我对java\Selenium非常陌生。我试图实现的是测试我们的网站是否在修补后恢复,需要做的是
1-开放式镀铬 2-打开url 3-登录 4-下载pdf
每次我的机器人遇到路障时,我都试图抓住一个错误。目前我有以下代码
当网站上弹出错误的密码提示时,写入代码的最佳方式是什么
如果这是一个如此简单的问题,请表示感谢和抱歉
package webdriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.io.*;
import java.lang.*;
import java.util.*;
public class launchbrowser {
public static WebDriver driver = null;
public static void main(String[] args) throws IOException {
File file1 = new File("out.txt");
FileWriter fw = new FileWriter(file1);
PrintWriter pw = new PrintWriter(fw);
try {
//Adds driver ver - dont remove
System.setProperty("webdriver.chrome.driver",".\\drivers\\chromedriver78.exe" );
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
}
//Catch the Error
catch (Exception e){
pw.println("unable to launch browser");
}
try {
//open webpage and maximizes the window
driver.navigate().to("****Webpage****");
driver.manage().window().maximize();
}
//Catch the Error
catch (Exception e){
pw.println("Unable to open Webpage");
}
try {
//locate Member Number\email
WebElement Username=driver.findElement(By.id("Login.Member number or email"));
Username.sendKeys("****test username****");
}
//Catch the Error
catch (Exception e){
pw.println("Unable to enter username");
}
try {
//locate password
WebElement Password=driver.findElement(By.id("Login.Password"));
Password.sendKeys("****test password****");
}
//Catch the Error
catch (Exception e){
pw.println("Unable to enter password");
}
//Click Login
WebElement Enter=driver.findElement(By.id("Login.Password"));
Enter.sendKeys(Keys.RETURN);
try {
//Download a statement
WebElement Transactions=driver.findElement(By.id("TransactionSummary"));
Transactions.click();
WebElement PDF=driver.findElement(By.linkText("Go"));
PDF.click();
}
catch (Exception e){
pw.println("Unable to download statement");
}
pw.close();
}
}
# 1 楼答案
根据您想要测试的内容,有两种方法可以实现:
在第一个选项中,我假设您的验证工作正常。第二个是检查你的验证工作是否正常