|
5#
楼主 |
发表于 2014-8-7 10:33:52
|
只看该作者
selenium登录流程 可以参数化吗
如一个java怎么写登录成功、登录失败呢
bokewangyu 发表于 2014-8-2 16:20
package com.webdriver.example;
import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import org.databene.benerator.anno.Source;
import org.databene.feed4junit.Feeder;
import org.junit.runner.RunWith;
import com.thoughtworks.selenium.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@RunWith(Feeder.class)
public class WebDriverlogin {
private static WebDriver driver;
private static String baseUrl;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://127.0.0.0";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
driver.quit();
}
@Source("username.xlsx")
@Test
public void test(String username,String password) {
driver.get(baseUrl + "/");
driver.findElement(By.name("username")).clear();
driver.findElement(By.name("username")).sendKeys(username);
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.cssSelector("input[type=\"submit\"]")).click();
if(username.contains("username1")){
SeleneseTestBase.seleniumEquals("xxxxxx", driver.getTitle());
driver.findElement(By.linkText("安全退出")).click();
}else if(username.contains("username2")||username.contains(" ")){
SeleneseTestBase.seleniumEquals("xxxxxx", driver.getTitle());
assertEquals("您操作失败了!可能因为:用户名错误!", driver.findElement(By.xpath("html/body/div[3]/div[1]/div")).getText());
}else if(username.contains("username3")){
SeleneseTestBase.seleniumEquals("xxxxxx", driver.getTitle());
assertEquals("您操作失败了!可能因为:密码错误!", driver.findElement(By.xpath("html/body/div[3]/div[1]/div")).getText());
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
} |
|