我在Selenium WebDriver中编写了一个测试。我创建的一个函数总是评估为false。
---测试用例类---
package com;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.Select;
public class EfroTechCareers {
private WebDriver driver;
public EfroTechCareers(WebDriver driver)
{
this.driver = driver;
if(!"Jobs and Career at Efrotech | Efrotech Services".equals(driver.getTitle()))
{
throw new IllegalStateException("This is not the Careers Page");
}
}
By applyingFor = By.id("ddlJobs");
By candidateName = By.id("txtName");
By candidateFatherName = By.id("txtFName");
By candidateGender = By.id("ddlGender");
By candidateAddress = By.id("txtAddress");
By candidateCity = By.id("ddlResumeCity");
By candidateEmail = By.id("txtEmail");
By candidateHomePhone = By.id("txtPhoneHome");
By candidateOfficePhone = By.id("txtPhoneOff");
By candidateCellPhone = By.id("txtPhoneMob");
By candidateCNIC = By.id("txtCNIC");
By candidateExperience = By.id("ddlExperience");
By candidateCurrentSalary = By.id("txtCurrentSalary");
By candidateExpectedSalary = By.id("txtExpectedSalary");
By btnFormReset = By.id("btnReset");
public EfroTechCareers formFillCareers()
{
driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[height='520']")));
new Select(driver.findElement(applyingFor)).selectByVisibleText("QA Engineer");
driver.findElement(candidateName).sendKeys("Candidate Name");
driver.findElement(candidateFatherName).sendKeys("Cand. Father's Name");
new Select(driver.findElement(candidateGender)).selectByVisibleText("Male");
driver.findElement(candidateAddress).sendKeys("XYZ Lane 123 Road");
new Select(driver.findElement(candidateCity)).selectByVisibleText("Karachi");
driver.findElement(candidateEmail).sendKeys("name@domainname.com");
driver.findElement(candidateHomePhone).sendKeys("12345678");
driver.findElement(candidateOfficePhone).sendKeys("87654321");
driver.findElement(candidateCellPhone).sendKeys("03331234567");
driver.findElement(candidateCNIC).sendKeys("12345-6789458-9");
new Select(driver.findElement(candidateExperience)).selectByVisibleText("5 Years");
driver.findElement(candidateCurrentSalary).sendKeys("123456");
driver.findElement(candidateExpectedSalary).sendKeys("654123");
driver.findElement(btnFormReset).click();
return this;
}
public boolean verifyFormReset()
{
if (
("-Select-".equals(new Select(driver.findElement(applyingFor)).getFirstSelectedOption().getText()) &&
"".equals(driver.findElement(candidateName).getText()) &&
"".equals(driver.findElement(candidateFatherName).getText()) &&
"-Select-".equals(new Select(driver.findElement(candidateGender)).getFirstSelectedOption().getText()) &&
"".equals(driver.findElement(candidateAddress).getText()) &&
"-Select-".equals(new Select(driver.findElement(candidateCity)).getFirstSelectedOption().getText()) &&
"".equals(driver.findElement(candidateEmail).getText()) &&
"".equals(driver.findElement(candidateHomePhone).getText()) &&
"".equals(driver.findElement(candidateOfficePhone).getText()) &&
"".equals(driver.findElement(candidateCellPhone).getText()) &&
"".equals(driver.findElement(candidateCNIC).getText()) &&
"-Select-".equals(new Select(driver.findElement(candidateExperience)).getFirstSelectedOption().getText()))
)
return true;
else
return false;
}
}
=====================
---这是主要的课程 -
package com;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.PageFactory;
public class EfroTechMain {
/**
* @param args
*
*/
public static WebDriver driver;
public static String baseUrl;
public static void main(String[] args) {
// TODO Auto-generated method stub
baseUrl = "http://www.efrotech.com";
driver = new HtmlUnitDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(baseUrl +"/careers");
EfroTechCareers eTC = PageFactory.initElements(driver, EfroTechCareers.class);
eTC.formFillCareers();
if (eTC.verifyFormReset() == true)
{
System.out.println("All fields are reset to defaults");
} else
System.out.println("Some fields are not reset to defaults");
}
}
=====================
方法verifyFormReset()始终求值为false,如果在if()块中仅检查单个语句,则有时求值为true。
在单个main()类中创建一次相同的方法,用于校正布尔值。
答案 0 :(得分:0)
尝试创建方法,并打印出结果以查看错误的方法。
您可以使用以下方法:
public Boolean verifyElementEmpty(By by, String s) {
Boolean check = driver.findElement(by).getText().equals(s);
System.out.println("Element: " + by.toString() + " Empty: " + check);
return check;
}
答案 1 :(得分:0)
问题已经解决了。问题是正在执行快速执行的HTMLUnitDriver驱动程序,并且我必须比较的值与预期值不匹配,导致方法总是计算为false。使用FirefoxDriver就可以了。
感谢yall的回复。