业务场景:在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(); } }
时间: 2024-10-12 16:53:27