我们正在尝试在基于交易的网站上开始实施回归测试的自动化。我成功设置了testng xml来打开firefox,IE和Chrome。它将运行事务类。在带参数的setUP方法中,它检查浏览器并为该线程打开正确的浏览器。然后将它定向到main()方法测试,该测试在返回到事务类以实际执行事务测试之前启动Login类。这样做的原因是有一个有组织的结构,每个类只有一个测试用于调试目的。
当我使用单个用户凭据运行此测试时,它运行正常并完成两个测试。问题是我们的系统允许用户每10秒输入一次事务(只是我们的一个安全功能,以避免刷新或提交多个事务)。无论如何,使用多个用户凭据的最佳方法是什么?我尝试了多种逻辑,但每当Firefox使用Username1时,IE和Chrome都会使用用户名2或3.
换句话说
或
我提出了几个成功的解决方案,但我不得不复制类或代码。
因此,一个解决方案是为每个浏览器创建单独的Login类,以及为每个浏览器创建单独的Transaction类。在testng中,我为每个浏览器运行了适当的类。我认为这是一个糟糕的解决方案,虽然它有效。
我提出的另一个解决方案是在检查每个浏览器时采用登录测试方法并将其放入Transaction类的parameters方法中的每个if()语句中。然后每个if语句都有其正确的用户名。
但是我不喜欢这个解决方案,因为如果我运行单个测试来检查正在开发的函数的新代码,我将不得不将这个相同的代码添加到我将创建的所有未来测试类中。它只是重复的代码,我认为应该在它自己的类中。
这是转换为testng之前的Login和Transaction类。另外,我创建了一个包含用户名和网址等静态变量的Environment类。
P.S。我删除了一堆这个问题不需要的测试代码。
例如:
static {
URL = "https://website.com";
DEFAULT_WAIT_TIME = 60;
SYSTEM_ID = "IDnumber";
USERNAME = "Username1";
PASSWORD = "Password1";
}
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Login {
private WebDriver driver;
private String baseURL;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
//driver = new FirefoxDriver();
//baseURL = Environment.URL;
driver.manage().timeouts().implicitlyWait(Environment.DEFAULT_WAIT_TIME, TimeUnit.SECONDS);
}
public static void doLogin(WebDriver driver, String baseURL) {
WebDriverWait wait = new WebDriverWait(driver, Environment.DEFAULT_WAIT_TIME);
System.out.println("Log in to trunk...");
driver.get(baseURL);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("company_id")));
if (driver.findElement(By.xpath("//div[span='Log In']/span")).isEnabled()) {
System.out.println(" 1 - 'Login' page is opened");
} else {
System.out.println(" !ERROR! - Can NOT open 'Login' page");
driver.findElement(By.id("This is to throw exception if Can NOT open 'Login' page"));
}
driver.findElement(By.id("company_id")).clear();
driver.findElement(By.id("company_id")).sendKeys(Environment.SYSTEM_ID);
driver.findElement(By.xpath("//input[@name='username']")).clear();
driver.findElement(By.xpath("//input[@name='username']")).sendKeys(Environment.USERNAME);
driver.findElement(By.id("c_password")).clear();
driver.findElement(By.id("c_password")).sendKeys(Environment.PASSWORD);
System.out.println(" 2 - Info filled");
//I removed the rest of the code that finishes the login because
//I don't think it is needed for demonstration
}
@Test
public void main() {
doLogin(driver, baseURL);
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
}
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Transaction {
private WebDriver driver;
private String baseURL;
private StringBuffer verificationErrors = new StringBuffer();
public static String country = "22";
public static String ammount = "500";
@Before
public void setUp() throws Exception {
// System.setProperty("webdriver.ie.driver", "../IEDriverServer.exe");
// driver = new InternetExplorerDriver();
driver = new FirefoxDriver();
baseURL = Environment.URL;
driver.manage().timeouts().implicitlyWait(Environment.DEFAULT_WAIT_TIME, TimeUnit.SECONDS);
}
public static void doTransaction(WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, Environment.DEFAULT_WAIT_TIME);
System.out.println("Performing transaction...");
// This is where the transaction code is ......
}
@Test
public void transaction() {
//Login first
Login.doLogin(driver, baseURL);
// Transaction
doTransaction(driver);
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
}
答案 0 :(得分:0)
您的示例代码是JUnit,但您提到您正在使用TestNG。在这种情况下,您可以使用@Parameters将不同的参数传递给测试。 Doc:http://testng.org/doc/documentation-main.html#parameters
示例:
的testng.xml
<suite>
<test name="firstTest">
<parameter name="username" value="username1"/>
<parameter name="password" value="password1"/>
....
</test>
<test name="secondTest">
<parameter name="username" value="username2"/>
<parameter name="password" value="password2"/>
....
</test>
</suite>
你的班级:
@Parameters({"username", "password"})
@BeforeTest
public void beforeTest(String username, String password) {
this.getDriver().get("http://mypage.com");
this.getDriver().findElement(By.id("usernameInputField")).sendKeys(username);
this.getDriver().findElement(By.id("passwordInputField")).sendKeys(password);
...
}
答案 1 :(得分:0)
创建自己的注释,例如
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TestCaseAuthentication {
String userID();
String password();
}
在每次输入用户/密码的测试中调用它:
@TestCaseAuthentication(password = "testtest", userID = "test")
从测试中获取值
if (method.isAnnotationPresent(TestCaseAuthentication.class)) {
TestCaseAuthentication userPassword = method.getAnnotation(TestCaseAuthentication.class);
if (userPassword != null) {
valueUserID = userPassword.userID();
valuePassword = userPassword.password();
} else {
fail("Invalid UserID or Password");
}
}
使用您的登录方法中的值。
如果您需要更多详细信息,请告诉我!