-1

私は、ページに正しいURLとタイトルがロードされているかどうかをチェックする方法を作成しようとしました。 私のコードは何回か、何度か倒れます。 これは私のエラーです:正しいURLとタイトルページを自動的に確認する

java.lang.AssertionError: Checking if http://store.demoqa.com/products-page/product-category/accessories/ contains: imacs 

は、これは私のコードです:

public void showNavigationLinks() { 

     Actions action = new Actions(driver); 
     String[] submenu = {"Accessories", "iMacs", "iPads" , "iPhones" , "iPods" , "MacBook"}; 

     for(int i=0; i < submenu.length; i++) { 

      WebElement productCategory = waitForElementToBeDisplayed(By.xpath("//a[contains(.,'Product Category')]"), 5000); 
      action = action.moveToElement(productCategory); 

      WebElement submenuItem = waitForElementToBeDisplayed(By.xpath("//a[contains(.,'" + submenu[i] + "')]"), 5000); 
      action.moveToElement(submenuItem).click().build().perform(); 

      String currentUrl = driver.getCurrentUrl(); 
      String title = driver.getTitle(); 

      Assert.assertTrue("Checking if " + currentUrl + " contains: " + submenu[i].toLowerCase(), 
        currentUrl.contains(submenu[i].toLowerCase())); 

      Assert.assertTrue("Checking if title contains: " + submenu[i], 
        title.contains(submenu[i])); 
      System.out.println(title); 
     } 
+1

[リンクであること、ナビゲーションを確認する可能性のある重複正しいページにつながる](http://stackoverflow.com/questions/41960839/verify-that-navigation-links-are-leading-to-correct-page) – mrfreester

+1

これが以前のものとどのように違うかを私が知ることは難しいあなたが尋ねた6つの質問?私は最近同じような質問が他の誰かによって提起されたのを見たと思います。重複を避けるためにいくつかの質問を削除してください。乾杯:) – mrfreester

答えて

0

あなたのアサーションが失敗したので、あなたがAssertionErrorがを得ている理由です。フェッチされるURLは "http://store.demoqa.com/products-page/product-category/accessories/"ですが、URLに "imacs"という単語が含まれることを期待しています(URLがhttp://store.demoqa.com/products-page/product-category/imacs/であると予想しています)、これによりアサーションが失敗します。

あなたのコードでは、サブメニュー項目 "imacs"が表示されるのを待っているので、サブメニューオプションをクリックし、サブメニューオプションのURLがロードされているかどうかを確認します。ページが読み込まれるまでに時間がかかることがあり、以前のURLを取得している可能性があるため、この操作では常に同じ結果が得られません。新しいページが完全に読み込まれてからページのURLとタイトルを取得するまで待つことをお勧めします。

-1

次試してみてください:{それはアサートを使用していないが、彼らはあなたのサブメニューエントリに一致している場合にのみ、それがタイトルとURLを出力します。

package package1; 

import java.awt.AWTException; 

import org.testng.Assert; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.openqa.selenium.interactions.Actions; 

public class new1 { 

static WebDriver driver = null; 

public static void main(String[] args) throws InterruptedException, AWTException { 

    System.setProperty("webdriver.chrome.driver","C:\\Eclipse\\Selenium\\chromedriver.exe"); 

    driver = new ChromeDriver(); 

    driver.manage().window().maximize(); 

    driver.get("http://store.demoqa.com/products-page/product-category/accessories/"); 

    Thread.sleep(5000); 

    showNavigationLinks() ; 

} 

public static void showNavigationLinks() { 

    Actions action = new Actions(driver); 
    String[] submenu = {"Accessories", "iMacs", "iPads" , "iPhones" , "iPods" , "MacBook"}; 

    for(int i=0; i < submenu.length; i++) { 

     WebElement productCategory = driver.findElement(By.xpath("//a[contains(.,'Product Category')]")); 
     action = action.moveToElement(productCategory); 

     WebElement submenuItem = driver.findElement(By.xpath("//a[contains(.,'" + submenu[i] + "')]")); 
     action.moveToElement(submenuItem).click().build().perform(); 

     String currentUrl = driver.getCurrentUrl(); 
     String title = driver.getTitle(); 

     if(currentUrl.contains(submenu[i].toLowerCase())){ 

      System.out.println("CurrentUrl is " + currentUrl + " " + submenu[i].toLowerCase()); 

     } 



     if(title.contains(submenu[i])){ 

      System.out.println("title is " + title + " " + submenu[i]); 

     } 



    } 

} 

}

+0

このコードは動作しますが、期待どおりに動作するとは限りません。テストが合格すると、すべての当事者が期待しているかどうかをチェックしません。私はそのアイテムを待つのを待つか、それが役に立たなかった。 –

+0

私は「すべての当事者が期待していることをテストに合格することはできません。」と説明してもらえませんか? – kushal

+0

私の問題は、すでにロードされていないことをチェックして、正しい側がエラーをスローしているかどうかをチェックすることです。私は待つことを試みたが、それは助けにはならなかった。私は waitForElementToBeDisplayedを使用しました。 –

関連する問題