如何为所有相同的类元素的相同过程编写脚本?

时间:2018-11-23 10:50:26

标签: java arrays selenium selenium-webdriver automation

我是使用Java的Selenium Webdriver的新手。我的<a class="row-title">属性最多可包含21个web元素。我想循环执行代码,以便可以将Java硒代码合并为较小的代码,目前,我必须为所有21个测试编写相同的脚本。例如。

<a class="row-title">1</a>>
........
........
<a class="row-tilte">21</a>.

also i have to do same clicking and update procedure up to 1 to 21 


package Dev_admin;
import java.nio.channels.SelectableChannel;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
public class present extends login{
@Test(priority = 1)
public void update1() {
    driver.findElement(By.xpath(".//*[@id='post-1217']/td[1]/strong/a")).click();
    WebDriverWait wait = new WebDriverWait(driver, 20);
    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='menu-posts-presentation']/a/div[3]")));
    element.click();
        }
@Test(priority = 2)
public void update2(){
    driver.findElement(By.xpath(".//*[@id='post-1139']/td[1]/strong/a")).click();
    WebDriverWait wait = new WebDriverWait(driver, 20);
    WebElement element = 
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//* 
    [@id='menu-posts-presentation']/a/div[3]")));
    element.click();        


//if i am using class name instead of x path , then how to do same procedure 
in loop so my code became so small or merged..

@Test(priority = 1)
    public void update1() {
    driver.findElement(By.className("row-title")).click();
    WebDriverWait wait = new WebDriverWait(driver, 20);
    WebElement element = 
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//* 
    [@id='menu-posts-presentation']/a/div[3]"))); 
    element.click();
        }

@Test(priority = 2)
   public void update2(){
   driver.findElement(By.className("row-title")).click();
    WebDriverWait wait = new WebDriverWait(driver, 20);
    WebElement element = 
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//* 
    [@id='menu-posts-presentation']/a/div[3]")));
    element.click();        
}

2 个答案:

答案 0 :(得分:0)

修改的等待方法:

public WebDriverWait wait_sec(WebDriver driver, int sec) {
    return new WebDriverWait(driver, sec);
}

管理WebElement:

public WebElement get_element_by_classname (WebDriver driver, String classname) {
    WebElement element = driver.findElement(By.className(classname));
    return element;
}

public WebElement wait2element_and_get_element_by_xpath (WebDriver driver, String xpath) {
    WebElement element = wait_sec(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
    return element;
}

用法:

@Test
public void update_x() {
    get_element_by_classname(driver, "row-title").click();
    wait2element_and_get_element_by_xpath(driver, ".//*[@id='menu-posts-presentation']/a/div[3]").click();
}

答案 1 :(得分:0)

@Test
public void test() {
    WebDriverWait wait = new WebDriverWait(driver, 20);
    //Finding all elements and saving to the list (we expect list to have 21 element)
    List<WebElement> rowEls = driver.findElements(By.className("row-title"));
    //Looping through each of 21 element and doing necessary actions for every element inside the loop
    for (WebElement rowEl: rowEls) {
        rowEl.click();
        WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='menu-posts-presentation']/a/div[3]")));
        element.click();
    }
}

旁注:不要忘记在测试中添加至少一个断言。目前,它只需要点击几下,而无需验证任何内容。

相关问题