セレンJavaドロップダウン、チェックボックス、カレンダー、自動補完



目次
  • Dropdowns
  • static Dropdown with Select
  • Dropdown by ID
  • autocomplete Option
  • Checkboxes
  • check is element is enabled, selected or displayed
  • calendar

  • ドロップダウン

    選択による静的ドロップダウン
    WebElement staticDropdown = driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency"));
    
    Select dropdown = new Select(staticDropdown);
    
    dropdown.selectByIndex(3);        Assert.assertEquals(dropdown.getFirstSelectedOption().getText(),"USD");
    
    dropdown.selectByVisibleText("AED");        Assert.assertEquals(dropdown.getFirstSelectedOption().getText(),"AED");
    
    dropdown.selectByValue("INR");     Assert.assertEquals(dropdown.getFirstSelectedOption().getText(),"INR");
    

    ドロップダウン
    //find element to click to open Dropdown
    driver.findElement(By.id("divpaxinfo")).click();
    //add an wait
    Thread.sleep(2000L);
    // click 6 times on a button
    for (int i = 1; i < 6; i++){
                driver.findElement(By.id("hrefIncAdt")).click();
            }
    Assert.assertEquals(driver.findElement(By.id("spanAudlt")).getText(), "6"); 
    driver.findElement(By.id("btnclosepaxoption")).click();
    

    自動補完オプション
    特殊テキストをクリックする
    // start input in input field
    driver.findElement(By.id("autosuggest")).sendKeys("ind");
    Thread.sleep(3000);
    //get the list of suggestet inputs 
    List <WebElement> options = driver.findElements(By.cssSelector("li.ui-menu-item a"));
    //loop through list of inputs an click specific Text + break out the loop
    for ( WebElement option : options ) {
      if (option.getText().equalsIgnoreCase("India")) {  
         option.click();
         break;
       }
    }
    
    矢を上下に動かす
    WebElement destination=driver.findElement(By.id("hp-widget__sTo"));
            destination.clear();
            destination.sendKeys("DEL");
            Thread.sleep(2000);
            destination.sendKeys(Keys.ARROW_DOWN);
            destination.sendKeys(Keys.ENTER);
    

    チェックボックス
    Assert.assertFalse(driver.findElement(By.cssSelector("[id*='SeniorCitizenDiscount']")).isSelected());
    
    driver.findElement(By.cssSelector("[id*='SeniorCitizenDiscount']")).click();   
    
    Assert.assertTrue(driver.findElement(By.cssSelector("[id*='SeniorCitizenDiscount']")).isSelected());
    
    カウントチェックボックス
    System.out.println(driver.findElements(By.cssSelector("#discount-checkbox input[type='checkbox']")).size());
            Assert.assertEquals(driver.findElements(By.cssSelector("#discount-checkbox input[type='checkbox']")).size(), 5);
    

    チェック要素は有効です
    isEnabled(); 
    //This method checks if an element is enabled. Returns true if enabled, else false for disabled.
    isSelected(); 
    // This method checks if element is selected (radio button,checkbox, and so on). It returns true if selected, else false for deselected
    isDisplayed();  
    //This method checks if element is displayed. In this recipe, we will use some of these methods to check the status and handle
    
    不透明度が減少した場合(他のボタンへのnudging)をチェックする
    System.out.println(driver.findElement(By.id("Div1")).getAttribute("style").contains("opacity: 0.5"));
    driver.findElement(By.id("ctl00_mainContent_rbtnl_Trip_1")).click();
    System.out.println(driver.findElement(By.id("Div1")).isEnabled());
    
     if (driver.findElement(By.id("Div1")).getAttribute("style").contains("opacity: 1")) {
       System.out.println("Calender enabled");
       Assert.assertTrue(true);
       } else {
       Assert.assertTrue(false)
       }
    
    

    カレンダー
    をクリックしてカレンダーボタンをクリックし、今日の日付をクリックするか、リスト内のすべての日付を収集目立つかを確認します.
    driver.findElement(By.id("travel_date")).click();
    
    //Grab common attribute of dates and put it into list and iterate
    List<WebElement> days = driver.findElements(By.xpath("//td[@class='day']"));
    
            for (int i = 0; i < days.size(); i ++){
               if( days.get(i).getText().equalsIgnoreCase("23"))
                {
                   days.get(i).click();
                }
            }
    
    more on calendar picking