简体   繁体   English

为什么我断言即使元素存在某个元素也包含某个String也会失败?

[英]Why is my assertion that the element contains a certain String failing even though it is there?

the assert at the end of the main method is failing, even though that string is contained within the element, at least according to the console write line i perform before the assert. 即使该字符串包含在元素中,main方法末尾的断言也会失败,至少根据我在断言之前执行的控制台写入行而言。

can anyone help me figure out why that assert is failing? 谁能帮我弄清楚为什么该断言失败了? I'm at my wit's end. 我机智的尽头。 Literally pulling my hair out. 从字面上拉我的头发。

And sorry if it's a mess, I am new to Java. 很抱歉,如果是一团糟,我是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']")));

}

} }

In the assertion you are calling toString() on WebElement , which returns something like: 在断言中,您正在WebElement上调用toString() ,返回的内容如下:

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

Just like Andy indicated, you should use itemParentRow.getText() . 就像Andy指出的那样,您应该使用itemParentRow.getText()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么即使 Firebase 数据快照包含子项,我的 ArrayList 也是空的 - why is my ArrayList empty even though firebase datasnapshot contains children 为什么我的流测试即使在某些节点上存在存储也无法检查某些节点中的事务? - Why are my flow tests failing to check the transaction in certain nodes even if they are present on their storages? 当我尝试添加ArrayList元素时,即使它是新的,为什么我的程序仍然存在? - Why does my program say the ArrayList element exists when I try to add it even though it's new? 赛普拉斯如何检查一个元素是否包含某个字符串 - Cypress how to check if an element contains a certain string 为什么我的方法说它必须返回一个类型字符串并给我一个错误,即使我的方法确实返回一个类型字符串 - Why does my method say it must return a type string and gives me an error even though my method does return a type string 比较两个字符串失败的断言 - Comparing two string failing assertion Selenium Java 字符串断言失败 - Selenium Java String assertion failing 为什么即使我的输入正确,也找不到我的班级? - Why are my classes not found even though my imports are correct? WebElement getText()返回空字符串,即使元素可见 - WebElement getText() returning empty string even though element is visible 为什么这个二进制搜索返回-1,即使该元素在数组中 - Why does this binary search return -1 even though the element is in the array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM