首页 > 代码库 > selenium+junit4实现参数化自动化测试
selenium+junit4实现参数化自动化测试
业务场景:在www.1905.com电影网中实现两个用户的登陆操作。
代码如下:
package com.m1905.junit;import java.util.Arrays;import java.util.Collection;import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.Parameterized;import org.junit.runners.Parameterized.Parameters;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebDriver.Navigation;import org.openqa.selenium.WebElement;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.WebDriverWait;@RunWith(Parameterized.class)public class LoginParameterTest { private static WebDriver driver; private static Navigation navigate; private static String url="http://www.1905.com"; @BeforeClass public static void setUpBeforeClass() throws Exception { driver = new FirefoxDriver(); navigate = driver.navigate(); navigate.to(url); driver.manage().window().maximize(); } private String username; private String password; public LoginParameterTest(String username,String password){ this.username = username; this.password = password; } @Parameters public static Collection<Object[]> prepareData(){ Object[][] object = {{"user1","pwd1"},{"user2","pwd2"}}; return Arrays.asList(object); } @Test public void testLogin() { try { Thread.sleep(8000); } catch (InterruptedException e) { e.printStackTrace(); } WebElement LogAndReg = driver.findElement(By.xpath(".//*[@id=‘site_nav_md‘]/ul/li[2]/a")); LogAndReg.click(); WebElement usernameBox = driver.findElement(By.xpath(".//*[@id=‘inputUsername‘]")); WebElement passwordBox = driver.findElement(By.xpath(".//*[@id=‘inputPassword‘]")); WebElement loginButton = driver.findElement(By.xpath(".//*[@id=‘loginreg‘]/div/div[1]/form/p/button")); usernameBox.clear(); usernameBox.sendKeys(username); passwordBox.sendKeys(password); loginButton.click(); WebDriverWait wait = new WebDriverWait(driver,10); WebElement logoutButton = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id=‘site_nav_md‘]/ul/li[3]/a[2]"))); logoutButton.click(); } @AfterClass public static void tearDownAfterClass() throws Exception { driver.close(); }}
selenium+junit4实现参数化自动化测试
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。