目前我正致力于自动化测试解决方案的实施。我决定选择htmlelements但是在这个实现过程中,我使用干净的webdriver + pagefactory进行编码,以了解htmlelements的优点。我不是很擅长编码,而且我几乎开始陷入困境。 我已经在http://htmlelements.qatools.ru/的介绍中实现了java类。
这是我的代码:
的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>w4w</groupId>
<artifactId>w4w</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.48.2</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ru.yandex.qatools.htmlelements</groupId>
<artifactId>htmlelements-java</artifactId>
<version>1.15</version>
</dependency>
<dependency>
<groupId>ru.yandex.qatools.ashot</groupId>
<artifactId>ashot</artifactId>
<version>1.4.12</version>
</dependency>
</dependencies>
</project>
LoginForm.java(Elements)
package htmlelements.Elements; /**
* Created by Asus on 06.12.2015.
*/
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import ru.yandex.qatools.htmlelements.annotations.Name;
import ru.yandex.qatools.htmlelements.element.Button;
import ru.yandex.qatools.htmlelements.element.HtmlElement;
import ru.yandex.qatools.htmlelements.element.TextInput;
@Name("Login Form")
public class LoginForm extends HtmlElement {
@Name("Username textbox")
@FindBy(id = "i_user")
private TextInput UserNameTextbox;
@Name("Password textbox")
@FindBy(id = "i_password")
private TextInput PasswordTextbox;
@Name("Login button")
@FindBy(xpath = "//*[@Value='Login']")
private Button LoginButton;
public void Login(String userName, String password){
UserNameTextbox.sendKeys(userName);
PasswordTextbox.sendKeys(password);
LoginButton.click();
}
}
LoginPage(PageObject):
package htmlelements.PageObjects;
import htmlelements.Elements.LoginForm;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import ru.yandex.qatools.htmlelements.annotations.Name;
import org.openqa.selenium.WebDriver;
import ru.yandex.qatools.htmlelements.element.TextInput;
import ru.yandex.qatools.htmlelements.loader.HtmlElementLoader;
/**
* Created by Asus on 06.12.2015.
*/
@Name("Login Page")
public class LoginPage {
private LoginForm loginForm;
public LoginPage(WebDriver driver){
HtmlElementLoader.populatePageObject(this, driver);
}
@FindBy(id = "i_user")
public TextInput usernm;
public void Login(String userName, String password){
loginForm.Login(userName, password);
}
}
MSWhtmlelements(试验):
/**
* Created by Asus on 05.12.2015.
*/
import htmlelements.PageObjects.LoginPage;
import htmlelements.PageObjects.MainMenuPage;
import htmlelements.PageObjects.MerchantServiceWorkbenchScreen;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
public class MSWhtmlelements {
private WebDriver driver;
@BeforeSuite
public void initDriver() throws Exception {
System.out.println("You are testing in firefox");
driver = new FirefoxDriver();
driver.manage().window().maximize();
}
@Test
public void trainingTest(){
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
LoginPage loginPage = new LoginPage(driver);
MainMenuPage mainMenuPage = new MainMenuPage(driver);
MerchantServiceWorkbenchScreen mswScreen = new MerchantServiceWorkbenchScreen(driver);
String baseLink = "http://w4w-auto:41600/";
driver.get(baseLink);
loginPage.usernm.sendKeys("123123");
loginPage.Login("epichugin", "epichugin");
mainMenuPage.GoTo();
}
@AfterSuite
public void quitDriver() throws Exception {
driver.quit();
}
}
所以,正如你所看到我试图调用动作2次: loginPage.usernm.sendKeys( “123123”); - 工作良好 loginPage.Login(“epichugin”,“epichugin”); - 根本不起作用。甚至没有例外。
对于webdriver + pageobjects,它的工作非常好且稳定。 这是我的pageobject类,它可以工作。对此的测试几乎相同:
/**
* Created by Asus on 04.12.2015.
*/
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class PageElements {
@FindBy(id = "i_user")
private WebElement UsernameTextbox;
@FindBy(id = "i_password")
private WebElement PasswordTextbox;
@FindBy(xpath = "//*[@Value='Login']")
private WebElement LoginButton;
public PageElements (WebDriver driver){
PageFactory.initElements(driver, this);
}
public void login (String username, String password){
UsernameTextbox.sendKeys(username);
PasswordTextbox.sendKeys(password);
LoginButton.click();
}
}
提前谢谢!
答案 0 :(得分:0)
您的LoginForm课程没有@FindBy
注释,您希望selenium如何找到您的登录表单?