java - 为什么我关于该元素包含某个字符串的断言失败了,即使它存在?

标签 java selenium selenium-webdriver selenium-chromedriver

主方法末尾的断言失败,即使该字符串包含在元素内,至少根据我在断言之前执行的控制台写入行。

谁能帮我弄清楚为什么该断言失败了?我已经无计可施了。简直就是把我的头发拔出来。

抱歉,如果有点困惑,我是 Java 新手。

enum Item {
   DRILL(100.00, "a0Gf40000005CctEAE", "Drill"), ///
   WRENCH(15.00, "a0Gf40000005CcuEAE", "Wrench"), ///
   HAMMER(10.00, "a0Gf40000005CcvEAE", "Hammer"); ///

private final double _price;
private final String _itemID;
private final String _itemDisplayName;

Item(double price, String itemID, String itemDisplayName){
    this._price = price;
    this._itemID = itemID;
    this._itemDisplayName = itemDisplayName;
}

double getItemPrice() {
    return _price;  
}

String getItemID() {
    return _itemID;     
}

String getItemName() {
    return _itemDisplayName;            
}
}


public class TestCaseOne {

 static WebDriver driver;


public static void main(String[] args) {

    // TODO Auto-generated method stub


    System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
    //System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
    driver = new ChromeDriver();    

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    Item testItem = Item.WRENCH;

    driver.get("http://test.com/WebOrderScreen");

    AddItem(testItem);
    ChangeItemQuantity(testItem, 3);        


    driver.findElement(By.xpath("//span[text()='Payment Information']")).click();                   

    WebElement itemLink =       driver.findElement(By.id("order-summary")).findElement(By.xpath(String.format(".//a[(@href='/%s')]", testItem.getItemID())));
    WebElement itemParentRow = itemLink.findElement(By.xpath("../../../.."));
    String text = itemParentRow.getText();
    System.out.println(text);

   Assert.assertTrue(itemParentRow.toString().contains(testItem.getItemName())); 

}


public static void AddItem(Item item) {

    //first find the link to the item we want
    WebElement itemLink = driver.findElement(By.xpath(String.format("//a[(@href='/%s')]", item.getItemID())));
    WebElement itemParentRow = itemLink.findElement(By.xpath("../../../.."));

    itemParentRow.findElement(By.tagName("i")).click(); //now that we found the parent row of the item we want, click the button to add it



    driver.findElement(By.id("shopping-cart")).findElement(By.xpath(String.format(".//a[(@href='/%s')]", item.getItemID())));
    System.out.println("Item ENUM found in shopping cart: " + item);

    ///for debugging 
    /*
    System.out.println(item.getItemID());
    System.out.println(item.getItemPrice());
    System.out.println(itemParentRow.getText());
    */




}


public static void ChangeItemQuantity(Item item, Integer quantity)  {


    WebElement itemLink =       driver.findElement(By.id("shopping-cart")).findElement(By.xpath(String.format(".//a[(@href='/%s')]", item.getItemID())));
    WebElement itemParentRow = itemLink.findElement(By.xpath("../../../../.."));


    //System.out.println("Old item count: " + itemParentRow.findElement((By.xpath(".//input[@inputmode='numeric']"))).getAttribute("value"));
    itemParentRow.findElement((By.xpath(".//input[@inputmode='numeric']"))).clear();
    itemParentRow.findElement((By.xpath(".//input[@inputmode='numeric']"))).sendKeys(quantity.toString());      
    //System.out.println("New item count: " + itemParentRow.findElement((By.xpath(".//input[@inputmode='numeric']"))).getAttribute("value"));

    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='shopping-cart']//td[@class='nx-summary']")));

}

}

最佳答案

在断言中,您在 WebElement 上调用 toString(),它返回如下内容:

[[[[[[ChromeDriver: chrome on LINUX (084b45f48be31410009e34b87903f54a)] ->     id: order-summary]] -> xpath: .//a[(@href='/a0Gf40000005CcuEAE')]]] -> xpath: ../../../..]

就像 Andy 指出的那样,您应该使用 itemParentRow.getText()

关于java - 为什么我关于该元素包含某个字符串的断言失败了,即使它存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47002851/

相关文章:

java - 当程序具有全局变量时,如何运行N个测试用例?

java - 使用 Tomcat 服务器从 Servlet 启动 Selenium Web 驱动程序时出错

c# - 保存 chrome cookies Selenium

java - 需要删除 Selenium 中的日志文件警告

ruby - 如何使用 Appium (Selenium Web-driver) 和 ruby​​ 获取浏览器的版本号

JAVA - hibernate - 在 DAO 中创建新查询

java - 替代已弃用的 setParameter(String name, Object val, Type type)

java - 如何在Android中获取带有按钮的项目信息

c# - 将代码块作为参数传递并在 try catch 中执行

python - selenium.common.exceptions.WebDriverException : Message: unknown error: Chrome failed to start: exited abnormally