2017-09-10 16 views
-1

以下のコードを使用してリンクページを開き、モバイルセクションに行き、名前順に基づいてアイテムをソートしようとしています。今、私は、モバイルデバイスがアルファベット順の名前でソートされているかどうかをチェックしたい。 私はそれらをループ、親切にこれを行うにはウェブ要素が整っているかどうかをチェックする方法は?

package selflearning; 

import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Collections; 
import java.util.HashSet; 
import java.util.List; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.Select; 

public class Guru99Ecommerce1 { 
    public static void main(String[] args) throws Exception { 
     System.setProperty("webdriver.gecko.driver","C:\\geckodriver\\geckodriver.exe"); 
     WebDriver driver = new FirefoxDriver(); 
     driver.get("http://live.guru99.com/index.php/"); 

     String title=driver.getTitle(); 
     String expectedTitle = "Home page"; 
     System.out.println("The title of the webPage is " + title); 
     expectedTitle.equalsIgnoreCase(title); 
     System.out.println("Title is verified"); 
     driver.findElement(By.xpath("//a[text()='Mobile']")).click(); 

     String nextTitle = driver.getTitle(); 
     System.out.println("The title of next page" + nextTitle); 

     String nextExpectedTitle = "pageMobile"; 
     nextExpectedTitle.equalsIgnoreCase(nextTitle); 
     System.out.println("The next title is verified"); 

     Select s = new Select(driver.findElement(By.xpath("//div[@class='category-products']//div/div[@class='sorter']/div/select[@title='Sort By']"))); 
     s.selectByVisibleText("Name"); 

     List<WebElement> element = driver.findElements(By.xpath("//div[@class='product-info']/h2/a")); 

     for(WebElement e: element) 
     { 
      String str = e.getText(); 
      System.out.println("The items are " + str); 
     } 
     HashSet<WebElement> value = new 
     List<WebElement> list = new ArrayList<WebElement>(element); 
     list.addAll(element); 
     System.out.println("arrangement" + list); 
    } 
} 
+0

はあなたが達成したいものを展示し、いくつかの例を追加してくださいすることができ

String url = "http://live.guru99.com/index.php"; driver.navigate().to(url); Assert.assertEquals(driver.getTitle(), "Home page"); driver.findElement(By.xpath("//nav[@id='nav']//a[.='Mobile']")).click(); Assert.assertEquals(driver.getTitle(), "Mobile"); sortBy("Name"); System.out.println(getProductNames()); System.out.println(isListSorted(getProductNames())); 

?リストは現在どのように見えますか?リストがソートされているかどうかを調べるには、その要素をトラバースして、要素が前の要素よりも大きいか等しいかをチェックします。または、ソートして、結果のリストが入力リストと等しいかどうかを確認することができます(注文を変更する可能性のある要素がない場合にのみ機能します)。 – Zabuza

+0

最後の行にコードがありません: 'HashSet value = new'。 – Zabuza

+0

私はハッシュセットをしようとしていましたが、達成することができませんでした –

答えて

1

最も簡単な方法は、単に製品のリストをつかむためにある助け、印刷された要素が昇順であるかどうかを確認することができArrayListに以下の私のリストを変換しようとしたが、ありません現在の製品名(String)がString#compareToIgnoreCase()を使用して最後の製品名より「大きい」かどうかを確認します。

このページで繰り返す可能性が高い一般的な作業のための関数をいくつか書きます。

public static void sortBy(String sortValue) 
{ 
    new Select(driver.findElement(By.cssSelector("select[title='Sort By']"))).selectByVisibleText(sortValue); 
} 

public static List<String> getProductNames() 
{ 
    List<String> names = new ArrayList<>(); 
    List<WebElement> products = driver.findElements(By.cssSelector("ul.products-grid h2.product-name")); 
    for (WebElement product : products) 
    { 
     names.add(product.getText()); 
    } 

    return names; 
} 

public static boolean isListSorted(List<String> list) 
{ 
    String last = list.get(0); 
    for (int i = 1; i < list.size(); i++) 
    { 
     String current = list.get(i); 
     if (last.compareToIgnoreCase(current) > 0) 
     { 
      return false; 
     } 

     last = current; 
    } 

    return true; 
} 

注:それはそれは非常に、非常に容易になります(そして、あなたが時間を節約した独自のものを書いてデバッグする必要はありません)ので、あなたが代わりにあなた自身を書いて、あなたの主張のためのJUnitやTestNGのを使用する必要があります。私が下に書いたコードはTestNGを使用しています。 TestNGのようなライブラリを使用している場合、以下のコードがどれほど短い(そしてより単純な)かを見ることができます。 getProductNames()戻り

[IPHONE, SAMSUNG GALAXY, SONY XPERIA] 
+0

@JainishKapadia 'getProductNames()'のコードは上記です。あなたが何を求めているのか分かりません。 – JeffC

関連する問題