Java 为什么我的元素包含某个字符串的断言即使存在也失败了?

Java 为什么我的元素包含某个字符串的断言即使存在也失败了?,java,selenium,selenium-webdriver,selenium-chromedriver,Java,Selenium,Selenium Webdriver,Selenium Chromedriver,主方法末尾的断言失败,即使该字符串包含在元素中,至少根据我在断言之前执行的控制台写入行 有人能帮我弄清楚为什么这个断言失败了吗?我束手无策。真的把我的头发拔出来了 很抱歉,如果这是一个混乱,我是新的Java enum Item { DRILL(100.00, "a0Gf40000005CctEAE", "Drill"), /// WRENCH(15.00, "a0Gf40000005CcuEAE", "Wrench"), /// HAMMER(10.00, "a0Gf40000

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

有人能帮我弄清楚为什么这个断言失败了吗?我束手无策。真的把我的头发拔出来了

很抱歉,如果这是一个混乱,我是新的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']")));

}

}

在您调用的断言中,toString on WebElement返回如下内容:

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

正如Andy指出的,您应该使用itemParentRow.getText。

您正在断言itemParentRow.toString包含,但正在打印itemParentRow.getText。您的意思是在断言中使用itemParentRow.getText吗?谢谢您的帮助。我知道这是很简单的事情。啊。