currently i'm working on automated testing solution implementation. I decided to choose htmlelements but during this implementation i'm coding using clean webdriver+pagefactory to see the advantages of htmlelements. I'm not really good at coding and i got stuck at the almost beginning. I've created java classes as was implemented in introduction on http://htmlelements.qatools.ru/.
Here is my code:
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(test):
/**
* 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();
}
}
So, as you see i tried to invoke actions 2 times : loginPage.usernm.sendKeys("123123"); - works fine loginPage.Login("epichugin", "epichugin"); - doesn't work at all. Even no exceptions appear.
In case of webdriver+pageobjects it works very good and stable. Here is my pageobject class which works. Test for this is almost the same:
/**
* 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();
}
}
Thank you in advance!
You don't have @FindBy
annotaion for your LoginForm class, how do you expect selenium would find your login form?