2017-01-05 8 views
0

このサイトのテスト:http://store.demoqa.com/Selenium 2にパラメータを持つメソッドを書き込む方法は?

私のテストでは、カートに商品を追加したり、カートから製品を取り外すことができます。

私はこのようになりますパラメータを指定しない方法を書いた:

public AllProductPage chooseProduct() { 
     //Click on product iPhone5 
     driver.findElement(By.className("wpsc_buy_button")).click(); 
     //Expected: Product "iPhone5" has been opened 
    return new AllProductPage(driver); 
    } 

を私はパラメータを持つメソッドを記述し、テストではなく、私が書いたコード内の製品を選択する必要があります。

@Test 
    public void verifyThatBeCanAddAndRemoveTheProductFromCart() throws InterruptedException { 

     ImplicitWait(driver); 

     HomePage onHomePage = new HomePage(driver); 
     System.out.println("Step 1"); 
     AllProductPage onAllProductPage = onHomePage.clickOnAllProduct(); 
     System.out.println("Step 2"); 
     onAllProductPage.chooseProduct(); 
     onAllProductPage.buttonGoToCheckout(); 
     onAllProductPage.submitForm(); 
     System.out.println("Step 3"); 
     Assert.assertTrue(onAllProductPage.getMessage().contains("Oops, there is nothing in your cart.")); 
    } 

答えて

0

私はあなたが関数に渡すしたいデータの種類がわからないんだけど、あなたは次のように、製品名を含む文字列を渡すことを試みることができます:

public AllProductPage chooseProduct(String productName) { 
    //Click on product received in parameter 
    driver.findElement(By.xpath("//div[contains(@class,'productcol')][descendant::*[contains(text(),'"+productName+"')]]//input[@class='wpsc_buy_button']")).click(); 
    //Expected: Product has been opened 
    return new AllProductPage(driver); 
} 
この例では、製品をテストコードに固定されているが、それは同様に可変とすることができることを

@Test 
    public void verifyThatBeCanAddAndRemoveTheProductFromCart() throws InterruptedException { 

     ImplicitWait(driver); 

     HomePage onHomePage = new HomePage(driver); 
     System.out.println("Step 1"); 
     AllProductPage onAllProductPage = onHomePage.clickOnAllProduct(); 
     System.out.println("Step 2"); 
     onAllProductPage.chooseProduct("iPhone 5"); 
     onAllProductPage.buttonGoToCheckout(); 
     onAllProductPage.submitForm(); 
     System.out.println("Step 3"); 
     Assert.assertTrue(onAllProductPage.getMessage().contains("Oops, there is nothing in your cart.")); 
    } 

注:

あなたのテストは次のようになります。

+0

これは私のエラーです:org.openqa.selenium.NoSuchElementException:そのような要素がありません:要素を見つけることができません: ""メソッド ":" xpath "、" selector ":" // div [contains(@class、 [iPhone 5])]] // input [@ name = 'wpsc_ajax_action'] "} –

+0

「iPHONE5」の代わりに「iPhone 5」を使用します。 xpath。関数に渡すテキストは、ページのように正確に一致する必要があります。 – Renato

+0

これは今エラーです:org.openqa.selenium.ElementNotVisibleException:要素が表示されません –

関連する問題