Selenium - 填写许多网站上的表格

时间:2016-08-04 07:49:52

标签: java selenium

我正在Selenium中创建我的脚本,该脚本填写在网站上。效果很好。 但我有几个网站,每种形式都不同,所以我有3个脚本,我必须单独打开。是否可以在一个脚本中自动化?

以下两个文件我必须单独打开:

package com.myform.tests.copy;
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.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class FillMyForm {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.example2.com");
        Thread.sleep(2000); 
        driver.findElement(By.id("emailfield")).sendKeys("test@example.com");
        driver.findElement(By.id("surnamefield")).sendKeys("John");
        driver.findElement(By.id("phonefield")).sendKeys("123123123");
        driver.findElement(By.id("agreefield")).click();            
        Thread.sleep(2000);
        driver.findElement(By.id("submitfield")).click();   
        Thread.sleep(2000);
        System.out.println(driver.getTitle());
        System.out.println(driver.findElement(By.id("result")).getText());      
    }
}


package com.myform.tests.copy.copy;
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.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class FillMyForm {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.example.com");
        Thread.sleep(2000); 
        driver.findElement(By.id("email")).sendKeys("test@example.com");
        driver.findElement(By.id("surname")).sendKeys("John");
        driver.findElement(By.id("phone")).sendKeys("123123123");
        driver.findElement(By.id("agree")).click();         
        Thread.sleep(2000);
        driver.findElement(By.id("submit")).click();    
        Thread.sleep(2000);
        System.out.println(driver.getTitle());
        System.out.println(driver.findElement(By.id("bd")).getText());      
    }
}

1 个答案:

答案 0 :(得分:0)

如果每个表单的执行相同,则可能需要为执行路径创建模板。获得模板后,您可以根据模板传递代码以执行。

以下是我尝试使用https://github.com/codezombies/easytest

所说的内容的一个示例
public static class PageActions {
    ActionableTemplate fillUpFormTemplate;
    ActionableTemplate afterFormSubmitTemplate;
}

public static void main(final String[] args) throws Exception {

    PageActions action1 = new PageActions();
    action1.fillUpFormTemplate = element -> {
        element.typeText("#emailfield", "test@example.com");
        element.typeText("#surnamefield", "John");
        element.typeText("#phonefield", "123123123");
        element.click("#agreefield");            
        element.click("#submitfield");     
    };
    action1.afterFormSubmitTemplate = element -> {
      System.out.println(element.get("#result"));  
    };

    PageActions action2 = new PageActions();
    action2.fillUpFormTemplate = element -> {
        element.typeText("#email", "test@example.com");
        element.typeText("#surname", "John");
        element.typeText("#phone", "123123123");
        element.click("#agree");            
        element.click("#submit");     
    };
    action2.afterFormSubmitTemplate = element -> {
        System.out.println(element.get("#bd"));  
    };

    final Map<String, PageActions> actions = new HashMap<>();
    actions.put("http://wwww.example1.com", action1);
    actions.put("http://wwww.example2.com", action2);

    for (Map.Entry<String, PageActions> entry : actions.entrySet()) {

        // execution template starts here
        EasyTestOptions options = new EasyTestOptions();
        options.addActionTemplate("fillUpForm", entry.getValue().fillUpFormTemplate);
        options.addActionTemplate("afterSubmit", entry.getValue().afterFormSubmitTemplate);

        try(EasyTest easy = new EasyTest(DriverType.FIREFOX)) {

            easy.start(entry.getKey());

            easy.newPage(page -> {
               page.executeTemplate("fillUpForm"); 
            });

            easy.newPage(page -> {
               page.executeTemplate("afterSubmit"); 
            });
        }
    }
}