Selenium使用要點記錄<三>

繼續很不負責任的接上回,前一篇文章裏邊提到了selenium處理popup window和等待加載的scenario.java

今天繼續討論一個使用selenium很容易遇到的問題和一個很不經常使用的問題,好吧,開始講起:測試

1.selenium處理下拉列表的各類方法ui

a.經過option元素裏面的value屬性值選中this

public void selectOptionByValue(WebElement select, String value, WebDriver driver) {
        //select = driver.findElement(By.id("id of select element")):
        List<WebElement> allOptions = select.findElements(By
                .tagName("option"));
        for (WebElement option : allOptions) {
            if (value.equals(option.getAttribute("value"))) {
                option.click();
                break;
            }
        }
    }

b.經過option元素顯示值選中google

public void selectOptionByVisibleText(String elementId, String visibleText){
        WebElement ele = driver.findElement(By.id(elementId));
        Select select = new Select(ele);
        select.selectByVisibleText(visibleText);
    }

c.經過opton在select中的index(從0開始)選中code

public void selectOptionByIndex(By by, String index){
        try{
            int ind = Integer.parseInt(index);
            WebElement ele = driver.findElement(by);
            this.selectOptionByIndex(ele, ind);
        }catch(Exception e){
            loggerContxt.error(String.format("Please configure a numeric as the index of the optioin for %s..",by.toString()));
            return;
        }
    }

d.來個下拉列表選中多個選項的狀況orm

/**
     * @elementId id of the select element
     * @param periodArr is the array of the indexes of the options in the dropdown list.
     */
    public void selectMultipleOptionsInDropdownList(String elementId, String[] periodArr){
        //你們自行傳入這個driver對象
        Actions actions = new Actions(this.driver);
        //我這裏單個的option選中是用的option的index經過xpath的方式定位的,你們能夠嘗試上邊其餘的方式
        for (int i = 0; i < periodArr.length; i++) {
            try{
                int num = Integer.parseInt(periodArr[i]);
                WebElement ele = driver.findElement(
                        By.xpath(String.format(".//select[@id='%s']/option[%d]",elementId,num)));
                actions.moveToElement(ele);
                if (!ele.isSelected()) {
                    actions.click();
                }
            }catch(Exception e){
                loggerContxt.info(String.format("Failed to parse the radia count::%s for Quater peroid.",periodArr[i]));
                continue;
            }
        }
        actions.perform();

暫時就列這麼些關於下拉列表的處理吧,這個挺好google,實在想偷懶的能夠給我留言。
對象

2.元素拖拽,感受在自動化測試裏邊這種需求比較少,可是我仍是碰到了的哈ip

      WebElement element1 = driver.findElement(By.id("element1"));
      WebElement element2 = driver.findElement(By.id("element2"));

      Actions actions = new Actions(driver);

      //選中須要拖動的元素,而且往x,y方向拖動一個像素的距離,這樣元素就被鼠標拉出來了,而且hold住
      actions.clickAndHold(element1).moveByOffset(1, 1);
      //把選中的元素拉倒目的元素上方,而且釋放鼠標左鍵讓需拖動元素釋放下去
      actions.moveToElement(element2).release();
      //組織完這些一系列的步驟,而後開始真實執行操做
      Action action = actions.build();
      action.perform();

其實Actions裏邊有public Actions dragAndDrop(WebElement source, WebElement target)這個方法能直接去拖拽元素,可能個人界面有點不太規範,用官方提供的這個一直不成功,因此我在這一系列的子操做之間加了一個小的步驟:選中source element以後往x,y放下拖動一個像素。element

你們稍微去看看Actions類裏邊還能發現不少關於元素操做的方法,但願你可能在裏邊能找到解決你需求的方法。

相關文章
相關標籤/搜索