如何在一个驱动程序实例中运行多个测试

时间:2014-05-30 06:22:48

标签: java excel selenium webdriver junit4

我有以下代码,我从excel表中提取数据并填写Web表单。它是一个注册表,并注册了两个用户。但是一旦第二次测试运行,它就会启动另一个驱动程序实例。我可以在一个例子中知道如何做到这一点。

@RunWith(Parameterized.class)
public class form {
private static WebDriver driver;

private String first;
private String last;
private String phone;
private String country;
private String about;

public form(String first, String last, String phone, String country, String about)
{
this.first = first;
this.last = last;
this.phone = phone;
this.country = country;
this.about = about;
}

@Before
public void before()
{
driver = new FirefoxDriver();
driver.manage().window().maximize();

}
@Parameters
public static Collection<Object[]> supplydata() throws IOException
{
File excel = new File("C:\\Users\\Master\\Desktop\\search_query_log.xls");
        FileInputStream fis = new FileInputStream(excel);
        HSSFWorkbook wb = new HSSFWorkbook(fis);
        HSSFSheet ws = wb.getSheet("Sheet1");

        int rowNum = ws.getLastRowNum() + 1;
        int colNum = ws.getRow(0).getLastCellNum();
        Object[][] data = new Object[rowNum][colNum];

        for (int i=0; i < rowNum ; i++)
        {
            HSSFRow row = ws.getRow(i);
            for(int j=0; j < colNum ; j++)
            {
                HSSFCell cell = row.getCell((short) j);
                if(cell.getCellType()==cell.CELL_TYPE_STRING)
                {
                data[i][j]=cell.getStringCellValue();
                }
                else if(cell.getCellType()==cell.CELL_TYPE_NUMERIC)
                {
                data[i][j]=String.valueOf(cell.getNumericCellValue());
                }               
            }
        }
        return Arrays.asList(data);
}



@Test
public void testcase() throws IOException, InterruptedException
{
driver.get("http://www.samplereg.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("fname")).sendKeys(first);
driver.findElement(By.name("lastname")).sendKeys(last);
driver.findElement(By.name("phonenumber")).sendKeys(phone);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

WebElement gender = driver.findElement(By.xpath("//input[@value='Male']"));
if (!gender.isSelected())
gender.click();
assertTrue(gender.isSelected());

driver.findElement(By.name("country")).sendKeys(country);
driver.findElement(By.name("desc")).sendKeys(about);

Select industry = new Select(driver.findElement(By.name("industry")));
assertFalse(industry.isMultiple());
assertEquals(6, industry.getOptions().size());

List<String> exp_options = Arrays.asList(new String[]{"Select Industry", "IT", "BPO","Sales","Development","Other"});
List<String> act_options = new ArrayList<String>();

for(WebElement option : industry.getOptions())
act_options.add(option.getText());
assertArrayEquals(exp_options.toArray(),act_options.toArray());

assertEquals("Select Industry", industry.getFirstSelectedOption().getText());
industry.selectByVisibleText("BPO");
assertEquals("BPO", industry.getFirstSelectedOption().getText());

Select education = new Select(driver.findElement(By.name("educationList")));
assertFalse(education.isMultiple());
assertEquals(4, education.getOptions().size());

List<String> exp_options1 = Arrays.asList(new String[]{"Select Education", "Post Graduate", "Graduate", "Under Graduate"});
List<String> act_options1 = new ArrayList<String>();

for(WebElement option1 : education.getOptions())
act_options1.add(option1.getText());
assertArrayEquals(exp_options1.toArray(),act_options1.toArray());

assertEquals("Select Education", education.getFirstSelectedOption().getText());
education.selectByVisibleText("Graduate");
assertEquals("Graduate", education.getFirstSelectedOption().getText());

WebElement hobby = driver.findElement(By.xpath("//input[@value='Listening Music']"));
                if (!hobby.isSelected())
                hobby.click();
                assertTrue(hobby.isSelected());

driver.findElement(By.cssSelector("input[type=file]")).sendKeys("C:\\Users\\Master\\Desktop\\image.jpg");
driver.findElement(By.cssSelector("input[type=submit]")).click();

}

@After
public void after()
{
//driver.quit();
}
}

1 个答案:

答案 0 :(得分:0)

如何尝试下面的内容。希望我的理解是正确的。

@BeforeClass
public static void setUpClass() {
    driver = new FirefoxDriver();
    driver.manage().window().maximize();

    Alert javascriptAlert = driver.switchTo().alert();
    System.out.println(javascriptAlert.getText()); // Get text on alert box
    javascriptAlert.accept(); // Chose whether to accept or cancel based on need
}

您可以删除@before方法。