2017-10-29 2 views
0

주 메소드의 끝에있는 어설 션은 적어도 어설트하기 전에 수행하는 콘솔 쓰기 라인에 따라 해당 문자열이 요소 내에 포함되어 있어도 실패합니다.요소가 거기에 있어도 특정 String을 포함하고 있다고 주장하는 이유는 무엇입니까?

누구나 내가 왜 그 주장이 실패하고 있는지 알아낼 수 있습니까? 나는 위트를 끝내고있다. 문자 그대로 내 머리카락을 꺼내.

죄송합니다. 엉망이라면 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']"))); 

} 

} 같은 반환 당신이 WebElementtoString()를 호출하는 주장에서,

+1

'itemParentRow.toString()'에 포함되어 있지만'itemParentRow.getText()'가 출력되어 있다고 주장하고 있습니다. 대신 어설 션에서'itemParentRow.getText()'를 사용하는 것을 의미합니까? –

+0

도움 주셔서 감사합니다. Andy. 나는 그게 뭔가 똑 바르다는 것을 알았습니다. 응. – kevin

답변

1

: 앤디 표시된 것처럼

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

, 당신은 itemParentRow.getText()를 사용해야합니다.

관련 문제