我想使用登录方法在每次测试时重复相同的代码。我在这里检查类似的主题,无法找到解决方案或只是不明白这一点。
测试是由Selenium IDE自动生成的,所以我对那里的某些方法有更多的疑问。我会标记让我困惑的事情。 这是我的测试和方法:
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Sortowanie{
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
String[] params;
@BeforeClass(alwaysRun = true)
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "https://en-testwebapi.poczta-polska.pl/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testSortowanie() throws Exception {
driver.get(baseUrl + "/");
login();
driver.findElement(By.xpath("(//input[@type='search'])[2]")).clear();
driver.findElement(By.xpath("(//input[@type='search'])[2]")).sendKeys("1");
driver.findElement(By.xpath("(//input[@type='search'])[3]")).clear();
driver.findElement(By.xpath("(//input[@type='search'])[3]")).sendKeys("09");
driver.findElement(By.xpath("(//input[@type='search'])[3]")).clear();
driver.findElement(By.xpath("(//input[@type='search'])[3]")).sendKeys("");
driver.findElement(By.xpath("(//input[@type='search'])[4]")).clear();
driver.findElement(By.xpath("(//input[@type='search'])[4]")).sendKeys("21");
driver.findElement(By.xpath("(//input[@type='search'])[4]")).clear();
driver.findElement(By.xpath("(//input[@type='search'])[4]")).sendKeys("");
}
@AfterClass(alwaysRun = true)
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
//I want to use params which will be defined in run configuration
public void login(){
driver.findElement(By.id("p")).clear();
driver.findElement(By.id("p")).sendKeys(params[1]);
driver.findElement(By.id("u")).clear();
driver.findElement(By.id("u")).sendKeys(params[0]);
driver.findElement(By.id("submit_button")).click();
}
//Where and when i need to use this method ??
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
//Where and when i need to use this method ??
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
//Where and when i need to use this method ??
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
为了和平,我重复我的问题: 如何制作获得params值的登录方法以及我需要实现的位置? 我在上面的代码评论中如何标记的工作方法。
任何帮助都将是有用的文章,教程,类似的分析代码。
感谢您的建议
答案 0 :(得分:1)
您可以使用页面工厂:
登录类
package locators;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class login {
@FindBy (id="p")
public WebElement password;
@FindBy (id="u")
public WebElement user;
public void Login(WebDriver driver, String login,String pass) {
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.id("p")));
password.clear();
password.sendKeys(pass);
user.clear();
user.sendKeys(login);
driver.findElement(By.id("submit_button")).click();
}}
你的考试
import java.util.concurrent.TimeUnit;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;
import locators.login; // if your test class and login class in the same package there is no need to import it
public class Sortowanie{
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
@BeforeClass(alwaysRun = true)
public void setUp() throws Exception {
driver = new ChromeDriver(); // for some reason FF don't want to load this page for me
baseUrl = "https://en-testwebapi.poczta-polska.pl/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
@Parameters({"login", "password"})
public void testSortowanie(String login, String password) throws Exception {
login L = PageFactory.initElements(driver, login.class); //
driver.get(baseUrl);
L.Login(driver, login, password);
}
@AfterClass(alwaysRun = true)
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
}
配置xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Sortowanie Test Suite" verbose="2">
<test name="Sortowanie Test 1" >
<parameter name="login" value="login 1"/>
<parameter name="password" value="pass 1"/>
<classes>
<class name="orders.test_setup"/>
<class name="Sortowanie"/>
</classes>
</test>
<test name="Sortowanie Test 2" >
<parameter name="login" value="login 2"/>
<parameter name="password" value="pass 2"/>
<classes>
<class name="orders.test_setup"/>
<class name="Sortowanie"/>
</classes>
</test>
</suite>