将Selenium WebDriver与Java一起使用
使用softassertion
声明测试用例,并在每个测试的末尾添加softassert.assertAll()
批量执行脚本时,测试用例的声明失败,如图所示。Actual Assertion Failed For This Test Case
在该测试用例之后,所有后续测试用例均失败,并显示如图所示的相同断言错误。All these Subsequent Tests got failed Showing Same Error
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
import utilities.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class UserCreationTest extends GetDriver {
static int status;
static String getUsername;
static String getName;
UserCreation userCreation = new UserCreation(driver, wait);
ParamValues paramValues = new ParamValues();
Common common = new Common(driver, wait);
SoftAssert softAssert = new SoftAssert();
JDBC_Connection db = new JDBC_Connection();
@Test
public void verifySelectPasswordPolicyDropdown() throws SQLException,
ClassNotFoundException {
CommonMethods.searchMenu(userCreation.menu);
CommonMethods.clickOnMenu(userCreation.menu, userCreation.landingPage);
CommonMethods.clickOnNewButton(userCreation.newButton);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(userCreation.pageLoad))).isDisplayed();
List<WebElement> selectValue = driver.findElements(By.xpath(userCreation.selectValue));
selectValue.get(0).click();
List<WebElement> policyList = driver.findElements(By.xpath(userCreation.passwordPolicyList));
int visiblePolicy = policyList.size();
int getPolicyCount = Integer.parseInt(db.setDataBase(SQLQueries.getPasswordPolicy));
softAssert.assertTrue(visiblePolicy == getPolicyCount, visiblePolicy + " is not equal to " + getPolicyCount);
ResultSet getPolicy = db.returnResultSet(SQLQueries.searchPolicy);
String getPolicyId = null;
List<String> getTooltipValue = new ArrayList<>();
while (getPolicy.next()) {
getPolicyId = getPolicy.getString(1);
getTooltipValue.add(getPolicy.getString(3));
getTooltipValue.add(getPolicy.getString(4));
getTooltipValue.add(getPolicy.getString(5).replace("1", "Yes").replace("0", "No"));
getTooltipValue.add(getPolicy.getString(6).replace("1", "Yes").replace("0", "No"));
getTooltipValue.add(getPolicy.getString(7).replace("1", "Yes").replace("0", "No"));
getTooltipValue.add(getPolicy.getString(8).replace("1", "Yes").replace("0", "No"));
getTooltipValue.add(getPolicy.getString(9).replace("1", "Yes").replace("0", "No"));
getTooltipValue.add(getPolicy.getString(10).replace("1", "Yes").replace("0", "No"));
getTooltipValue.add(getPolicy.getString(11).replace("1", "Yes").replace("0", "No"));
getTooltipValue.add(getPolicy.getString(12).replace("1", "Yes").replace("0", "No"));
getTooltipValue.add(getPolicy.getString(19).replace("1", "Yes").replace("0", "No"));
getTooltipValue.add(getPolicy.getString(13));
getTooltipValue.add(getPolicy.getString(14));
getTooltipValue.add(getPolicy.getString(21));
break;
}
driver.findElement(By.xpath(userCreation.selectSearchedPolicy[0])).sendKeys(getPolicyId);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(userCreation.selectSearchedPolicy[1]))).isDisplayed();
driver.findElement(By.xpath(userCreation.selectSearchedPolicy[1])).click();
softAssert.assertTrue(driver.findElement(By.xpath(userCreation.tooltip)).isDisplayed());
String[] tooltipVal = driver.findElement(By.xpath("//DIV[@class='tooltip-inner']")).getText().split("\n");
List<String> toolTipContent = new ArrayList<>();
for (int n = 0; n < tooltipVal.length; n++) {
String[] NewTooltip = tooltipVal[n].split(" - ");
try {
toolTipContent.add(NewTooltip[1]);
//System.out.println(NewTooltip[1]);
} catch (IndexOutOfBoundsException e) {
System.out.println(tooltipVal[n] + " is not available");
toolTipContent.add(n, "No");
}
}
//Compare DB vs UI Values
softAssert.assertTrue(toolTipContent == getTooltipValue, toolTipContent + " is not equal to " + getTooltipValue);
//A) No data found
selectValue.get(0).click();
driver.findElement(By.xpath(userCreation.selectSearchedPolicy[0])).sendKeys(TestData.updateData);
try {
softAssert.assertTrue(driver.findElement(By.xpath(userCreation.passwordPolicyList)).isDisplayed(), "No List Found for invalid Search");
} catch (Exception e) {
System.out.println("Element not found");
}
//B) Valid Search
String searchPolicy = db.setDataBase(SQLQueries.searchPolicy);
driver.findElement(By.xpath(userCreation.selectSearchedPolicy[0])).clear();
driver.findElement(By.xpath(userCreation.selectSearchedPolicy[0])).sendKeys(searchPolicy);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(userCreation.selectSearchedPolicy[1]))).isDisplayed();
softAssert.assertTrue(driver.findElement(By.xpath(userCreation.passwordPolicyList)).isDisplayed(), "No List Found for valid search");
softAssert.assertAll();
}
@Test
public void verifySelectLanguageDropdown() {
CommonMethods.searchMenu(userCreation.menu);
CommonMethods.clickOnMenu(userCreation.menu, userCreation.landingPage);
CommonMethods.clickOnNewButton(userCreation.newButton);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(userCreation.pageLoad))).isDisplayed();
driver.findElement(By.xpath(userCreation.selectLanguage)).click();
List<WebElement> languageList = driver.findElements(By.xpath(userCreation.languageList));
int visibleLanguage = languageList.size();
System.out.println(visibleLanguage + " languages are available");
//A) No data found
driver.findElement(By.xpath(userCreation.searchLanguage)).sendKeys(TestData.updateData);
try {
softAssert.assertTrue(driver.findElement(By.xpath(userCreation.languageList)).isDisplayed(), "No Language List Found for invalid search");
} catch (Exception e) {
System.out.println("Element not found");
}
//B) Valid Search
driver.findElement(By.xpath(userCreation.searchLanguage)).clear();
driver.findElement(By.xpath(userCreation.searchLanguage)).sendKeys(ParamValues.enterLanguage);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(userCreation.chooseSelectedLanguage))).isDisplayed();
softAssert.assertTrue(driver.findElement(By.xpath(userCreation.chooseSelectedLanguage)).isDisplayed(), "No Language list Found for valid search");
driver.findElement(By.xpath(userCreation.chooseSelectedLanguage)).click();
softAssert.assertAll();
}
请找到上面的代码
在这种情况下,方法verifySelectPasswordPolicyDropdown()的断言在以下位置失败
//Compare DB vs UI Values
softAssert.assertTrue(toolTipContent == getTooltipValue, toolTipContent + "
is not equal to " + getTooltipValue);
以下是失败的错误
java.lang.AssertionError: The following asserts failed:
[2, 2, No, No, No, No, No, No, No, No, No, 3, 3, 30] is not equal to [2, 2,
No, No, No, No, No, 5, No, No, No, 3, 3, 30] expected [true] but found
[false]
Expected :true
Actual :false
此后,方法verifySelectLanguageDropdown()会显示相同的错误,并且也会失败