How to handle multiple step definition file in Cucumber with Selenium - selenium-webdriver

I have two step definition files and in future, there will be multiple, I am unable to execute my code for logout, its working fine for login and one step definition file, and it continuously open chrome browser.
I have created a framework using page factory, below is my code:
Login Page:
public class LoginPage {
WebDriver driver;
public LoginPage(WebDriver driver) {
//this.driver=driver;
PageFactory.initElements(driver, this);
}
#FindBy(how=How.ID,using="username")
public WebElement usernametexbox;
#FindBy(how=How.ID,using="pass")
public WebElement passwordtextbox;
#FindBy(how=How.ID,using="submit")
public WebElement signin;
#FindBy(how=How.XPATH,using="//button[#class='btn']")
public WebElement acceptbutton;
public void enter_username(String username) {
usernametexbox.clear();
usernametexbox.sendKeys(username);
usernametexbox.getText();
}
public void enter_password(String password) {
passwordtextbox.clear();
passwordtextbox.sendKeys(password);
}
public void clickToSigninbutton() {
signin.click();
}
public void clickToAcceptbutton() {
acceptbutton.click();
}
public void fill_LoginDetails() {
enter_username("abc");
enter_password("def45");
}
}
Logout Page:
public class LogoutPage {
WebDriver driver;
public LogoutPage(WebDriver driver) {
//this.driver=driver;
PageFactory.initElements(driver, this);
}
#FindBy(how=How.XPATH,using="//*[#class='icon']")
public WebElement chevron;
#FindBy(how=How.XPATH,using="//a[#class='logout-link']")
public WebElement logoutlink;
public void clickTochevron() {
chevron.click();
}
public void clickToLogoutLink() {
link.click();
}
}
Property file reader:
public class PropertiesFileReader {
public Properties getproperty() throws IOException {
FileInputStream inputstream=null;
Properties properties=new Properties();
try {
properties.load(new FileInputStream("resources/config.properties"));
}catch(Exception e) {
System.out.println("Exception " +e);
}
return properties;
}
}
Browser utility:
public class BrowserUtility {
public static WebDriver openBrowser(WebDriver driver, String browserName, String url) throws InterruptedException {
if(browserName.equals("chrome")) {
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
return driver;
}else if(browserName.equals("IE")) {
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
Thread.sleep(5000);
return driver;
}else if(browserName.equals("Firefox")) {
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
Thread.sleep(5000);
return driver;
}
return driver;
}
}
Login Stepdef:
public class StepDefinition {
public static WebDriver driver;
// public LoginPage loginpage;
// Properties properties=new Properties();
PropertiesFileReader obj=new PropertiesFileReader();
#Given("^Open browser and enter url$")
public void open_browser_and_enter_url() throws Throwable {
Properties properties=obj.getproperty();
driver= BrowserUtility.openBrowser(driver, properties.getProperty("browser.Name"), properties.getProperty("URL"));
}
#Then("^Enter username and password$")
public void enter_username_and_password() throws Throwable {
LoginPage loginpage=new LoginPage(driver);
loginpage.fill_LoginDetails();
}
#Then("^click on sign in button$")
public void click_on_sign_in_button() throws Throwable {
new LoginPage(driver).clickToSigninbutton();
System.out.println("Sign-In successfully");
}
#Then("^Terms and conditions page open and click on Accept button$")
public void terms_and_conditions_page_open_and_click_on_Accept_button() throws Throwable {
new LoginPage(driver).clickToAcceptbutton();
}
}
Logout stepdef:
public class Logoutstepdef {
public static WebDriver driver;
PropertiesFileReader obj=new PropertiesFileReader();
#Given("^Chevron near username$")
public void chevron_near_username() throws Throwable {
Properties properties=obj.getproperty();
driver= BrowserUtility.openBrowser(driver,
properties.getProperty("browser.Name"), properties.getProperty("URL"));
LogoutPage logoutpage=new LogoutPage(driver);
logoutpage.clickTochevron();
}
#Then("^click on chevron and it should get expands$")
public void click_on_chevron_and_it_should_get_expands() throws
Throwable {
System.out.println("when user click on checvron it should
further expands a window");
}
#Then("^click on Logout link$")
public void click_on_Logout_link() throws Throwable {
new LogoutPage(driver).clickToLogoutLink();
}
}
Expected Results: Application should get automated successfully for different step definition files and only one browser should get opened at a time.
Actual Results: I have two step definition file and in future there will be multiple, I am unable to execute my code for logout, its working fine for login and one step definition file, and it continuously open chrome browser.

Your Then step is creating another instance of the page, that's why you get multiple browsers open.
Try doing this in your Logoutstepdef class:
public class Logoutstepdef {
public static WebDriver driver;
PropertiesFileReader obj=new PropertiesFileReader();
private LogoutPage logoutpage;
.....//the rest remains the same, until:
#Then("^click on Logout link$")
public void click_on_Logout_link() throws Throwable {
logoutpage.clickToLogoutLink();
}
}
There are similar questions here and here.

i would suggest to try gherkin with qaf. With QAF you don't need to manage driver or don't need to worry about page. All you need to extend WebdriverTestPage to your page class and you are done. You also can have steps in Page class itself. For example, with qaf your logout page may look like below:
public class LogoutPage extends WebDriverBaseTestPage<WebDriverTestPage>{
#FindBy(locator="xpath=//*[#class='icon']")
public WebElement chevron;
#FindBy(locator="xpath=//a[#class='logout-link']")
public WebElement logoutlink;
public void clickTochevron() {
chevron.click();
}
//#Then("^click on Logout link$") //you can use cucumber or qaf step annotation
#QAFTestStep(description="click on Logout link")
public void clickToLogoutLink() {
link.click();
}
}
Browser management (opening/closing browser) is taken care by QAF so depending on your configuration (sequential/parallel) qaf will provide thread safe drive sessions.
Furthermore, with locator repository you can eliminate one layer of page class. There are in-built steps available you can directly use. For example:
logoutpage.propeties
chevron.icon.loc=xpath=//*[#class='icon']
logout.link.loc=xpath=//a[#class='logout-link']
in your BDD you can directly use in-built steps as below:
Scenario: name of scenario
Given ...
When ...
Then verify 'chevron.icon.loc' is present
And click on 'logout.link.loc'
To make your locator self descriptive, you can go one step ahead as below:
logout.link.loc=xpath={"locator":"//a[#class='logout-link']","desc":"logout button"}
The description will be used in reporting to make it more meaningful.
There are lots of other feature that are crucial for functional test automation.

Related

Getting InstantiationException when creating factory for a page using appium

I am running a test case using pagefactory method and have created an appium driver. I'm trying to initialising a page using pagefactory class like this:
The test class:
public class VerifyValidLogin {
#Test
public void CheckValidUser() throws MalformedURLException {
AppiumDriver driver = DeviceFactory.CreateDriver();
login login_page = PageFactory.initElements(driver, login.class);
}
}
DeviceFactory class:
public class DeviceFactory {
public static AppiumDriver<MobileElement> driver;
public static AppiumDriver CreateDriver() throws MalformedURLException {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability( capabilityName: 'deviceName', value: 'Something');
...
URL url = new URL("http://127.0.0.1:4723/wd/hub");
driver = new AppiumDriver<MobileElement>(url,cap);
System.out.print("Application started");
return driver;
}
}
Login class has element locators:
public class login {
AppiumDriver driver;
public login(AppiumDriver ldriver)
{
this.driver=ldriver;
}
#FindBy(how = How.XPATH,using ="xpath");
MobileElement SignInButton;
}
But i'm not sure where i am doing wrong.
The error is
java.lang.RuntimeException: java.lang.InstantiationException: com.Demo.pages.login
at org.openqa.selenium.support.PageFactory.instantiatePage(PageFactory.java:134)
at org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:64)
at com.Demo.Testcases.VerifyValidLogin.CheckValidUser(VerifyValidLogin.java:18)
...
I am new to the automation testing so i'm not able to properly understand the error. Let me know if you need more details.
Initialise elements in constructor
public login(AppiumDriver ldriver)
{
this.driver=ldriver;
PageFactory.initElements(ldriver,this);
}

Cucumber #Before hook runs twice #After once

to all.
Curently writing a little BDD Test automation framework, using Java11+Junit5+Cucumber+Selenium, build tool: Graddle. Created a little test for validating Google title. When starting test, using Test task in Graddle or running CucumberRunner class, in both cases getting the same result: two times #Before method is executed, once #After method is executed and one browser windows is staying open. After added one more test, the same situation, only 4 browsers are opened, 2 of them are closing. Can anyone help with this situation?
Link to repository
After some watching of logs saw, that, seems, #Before is not executed twice, but Driver class is initialized twice, but why it happens no idea for now...
My code for now:
CucumberRunner.java:
#RunWith(Cucumber.class )
#CucumberOptions(
features = "src\\test\\java\\features",
glue = {"steps", "utils"},
tags = "#smoke")
public class CucumberRunner {
}
Driver.java:
public class Driver {
private WebDriver driver;
public Driver(){
driverInitialization();
}
private void driverInitialization(){
System.setProperty("webdriver.chrome.driver", "D:\\Soft\\selenium-drivers\\chromedriver.exe");
System.out.println("Starting driver.");
var browserName = "chrome";
switch (browserName.toLowerCase()){
case "chrome":
System.out.println("Starting chrome");
driver = new ChromeDriver();
System.out.println("Before break.");
break;
case "firefox":
driver = new FirefoxDriver();
break;
default:
throw new NotFoundException("Browser not found: " + browserName);
}
}
public WebDriver getDriver(){
return driver;
}
public WebDriverWait getWebDriverWait(){
return new WebDriverWait(driver, 120);
}
public void terminateDriver(){
System.out.println("Terminating driver.");
if (driver != null) {
driver.close();
driver.quit();
}
}
}
Hooks.java:
public class Hooks {
private Driver driver;
#Before
public void setup(){
System.out.println("In the Setup method.");
driver = new Driver();
}
#After
public void tearDown(){
System.out.println("In the TearDown method.");
driver.terminateDriver();
}
}
I think your Hook Class should be like this As You Are Using selenium-picocontainer DI.
public class Hooks {
private Driver driver;
public Hooks(Driver driver) {
this.driver = driver;
}
#Before
public void setup(){
System.out.println("In the Setup method.");
}
#After
public void tearDown(){
System.out.println("In the TearDown method.");
driver.terminateDriver();
}
}

How to solve error: Cannot Focus element - keyword driven framework in Selenium

I am using Keyword driven framework for automate Log in page for one of the site. here I have used Poi for accessing the Excel sheet Data.
Here Below method openBrowser() and openUrl() are working fine. Problem happened for username() method for accessing the username field. I am using Selenium 3.12 and Chrome driver version 2.39 compatible with Chorme
Here is my code :
public class ActionKeywords {
public static WebDriver driver;
public static void openBrowser(){
System.setProperty("webdriver.chrome.driver","E:\\Pratik_BackUP\\Automation Selenium\\Drivers\\chromedriver.exe");
driver = new ChromeDriver();
System.out.println("open Browser called");
}
public static void openUrl(){
driver.manage().timeouts().implicitlyWait(100,TimeUnit.SECONDS);
driver.get("correct url");
driver.manage().window().maximize();
System.out.println("open Url called");
}
public static void userName(){
driver.findElement(By.xpath(".//*[#id='ctl00_ContentPlaceHolder1_login1_pan']/div/table/tbody/tr[3]/td[1]")).sendKeys("admin");
System.out.println("user Name called");
}
public static void password(){
driver.findElement(By.xpath(".//*[#id='ctl00_ContentPlaceHolder1_login1_pan']/div/table/tbody/tr[4]/td[1]")).sendKeys("Camlin#357");
System.out.println("password called");
}
public static void click_Login(){
driver.findElement(By.xpath(".//*[#id='ctl00_ContentPlaceHolder1_login1_LoginButton']")).click();
System.out.println("LogIn called");
}
public static void waitFor() throws Exception{
Thread.sleep(5000);
}
public static void closeBrowser(){
driver.quit();
System.out.println("close Browser called.");
}
}
Thanks!!
public static void userName(){
WebElement exelement = driver.findElement(By.xpath(".//*[#id='ctl00_ContentPlaceHolder1_login1_UserName']"));
exelement.clear();
Actions actions = new Actions(driver);
actions.moveToElement(exelement);
actions.click();
actions.sendKeys("admin");
actions.build().perform();
}
Above code works fine. Here have used Actions class for solve Actions class

Selenium POM java.lang.NullPointerException

I am getting "java.lang.NullPointerException" when I am trying to execute my test case based on POM.
The class BrowserFactory lets me choose a browser, the class Flipkart_Login based on POM stores all the element of that particular page and has a method for Valid_Login()
and finally Test_Flipkart_Login class - calls the Valid_Login() method for executon but when I try to execute this class, I get java.lang.NullPointerException.
Kindly advise!
FAILED: Flipkart_Login_Test
java.lang.NullPointerException
at DataProviders.ConfigDataProvider.getURL(ConfigDataProvider.java:31)
at TestCases.Test_Flipkart_Login.Flipkart_Login_Test(Test_Flipkart_Login.java:19)
public class ConfigDataProvider
{
static Properties pro;
public ConfigDataProvider()
{
File src = new File("C:\\Data\\Bimlesh\\Flipkart_HybridFramework\\Flipkart.Hybrid.FrameworkComplete\\Configuration\\Config.Properties");
try
{
FileInputStream fis = new FileInputStream(src);
pro = new Properties();
pro.load(fis);
} catch (Exception e)
{
System.out.println("The Config exception is :"+e.getMessage());
}
}
public static String getURL()
{
String URL = pro.getProperty("URL");
return URL;
}
public static String ChromePath()
{
String Chrome = pro.getProperty("Chromepath");
return Chrome;
}
public static String IEPath()
{
String IE = pro.getProperty("IEpath");
return IE;
}
}
public class BrowserFactory
{
static WebDriver driver;
public static WebDriver getBrowser(String BrowserName)
{
if(BrowserName.equalsIgnoreCase("Firefox"))
{
driver = new FirefoxDriver();
}
else if(BrowserName.equalsIgnoreCase("Chrome"))
{
System.setProperty("webdriver.chrome.driver", ConfigDataProvider.ChromePath());
driver = new ChromeDriver();
}
else if(BrowserName.equalsIgnoreCase("IE"))
{
System.setProperty("webdriver.ie.driver", ConfigDataProvider.IEPath());
driver = new InternetExplorerDriver();
}
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
return driver;
}
public void ShutDown(WebDriver driver)
{
driver.quit();
}
}
public class Flipkart_Login
{
WebDriver driver;
public Flipkart_Login(WebDriver driver)
{
this.driver=driver;
}
#FindBy(xpath="//a[text()='Log In']") WebElement Login_Click;
#FindBy(xpath="//input[#class='_2zrpKA' and #type='text']") WebElement Enter_Email;
#FindBy(xpath="//input[#class='_2zrpKA _3v41xv' and #type='password']") WebElement Enter_Pass;
#FindBy(xpath="//button[#type='submit' and #class='_3zLR9i _1LctnI _36SmAs']") WebElement Login_Button;
public void Valid_Login()
{
Login_Click.click();
Enter_Email.sendKeys("xxx#gmail.com");
Enter_Pass.sendKeys("xxx");
Login_Button.click();
}
}
public class Test_Flipkart_Login
{
WebDriver driver;
#Test
public void Flipkart_Login_Test()
{
driver = BrowserFactory.getBrowser("Firefox");
driver.get(ConfigDataProvider.getURL());
Flipkart_Login page1 = PageFactory.initElements(driver, Flipkart_Login.class);
page1.Valid_Login();
}
}
You have initialized Properties pro in the constructor of the COnfigDataProider but you are using a static call to getURL method from your test class. Thus pro will be null and not initialized. Remove static call and use the constructor or make pro to static and initialize in static block.

How to get page object relative to another page object in Selenium WebDriver

I'm looking for solution how to get page object relative to another object in Page Object Model for selenium webdriver
Code of my test:
class StartPage {
WebDriver driver;
public HomePage(driver) {
this.driver = driver;
}
#FindBy(xpath="//div[#class='widget']//a[text()='link text']")
WebElement linkInWidget;
public void clickLink() {
linkInWidget.click();
return PageFactory.initElements(driver, NextPage.class);
}
}
Next page
class NextPage {
WebDriver driver;
public HomePage(driver) {
this.driver = driver;
}
#FindBy(xpath="//div[#class='widget']//input[#type='button']")
WebElement buttonInWidget;
#FindBy(id = "Index")
WebElement index;
public void clickButton() {
buttonInWidget.click();
return PageFactory.initElements(driver, NextPage.class);
}
public String getText() {
return index.getText();
}
}
Configuration class
public class ConfigureTest{
protected WebDriver driver;
protected String baseUrl;
protected StartPage startPage;
protected NextPage nextPage ;
#BeforeSuite
public void setUp() {
baseUrl = "http://webapp.com/";
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#AfterSuite
public void tearDown() throws Exception {
driver.quit();
}
}
And class of my test
public class SomeTest extends ConfigureTest {
#Test
public void testLinkAndButton() throws Exception {
startPage = PageFactory.initElements(driver, SomePage.class);
driver.get(baseUrl);
nextPage = startPage.clickLink();
nextPage.clickButton();
String data = nextPage.getText();
}
}
In both classes FirstPage and NextPage i find elements by xpath which has same first part //div[#class='widget'] it mean that all elements like buttons and links are under this widget and i have same xpath for widgets in my all pages
Problem: if only xpath of my widget will be changed i must make changes in all page objects of my test
Question: Is in any way how to improve my test for more flexibility with usage like:
page().get(Widget.class, "Widget name").get(Button.class, "Button name").click
Update: I resolve part of this problem in such way:
I create classes of my UI elements with get methods which can return objects of any class:
Class of Widget object
public class widget{
WebDriver driver;
public widget (WebDriver driver) {
this.driver = driver;
}
// Find a single element
#FindBy(xpath="//div[#class='tsf-p']")
WebElement linkInWidget;
public void click() {
linkInWidget.click();
}
public <T> T get(Class<T> expectedPage, String uiclass){
return PageFactory.initElements(driver, expectedPage);
}
}
Class of Button object
public class Button {
WebDriver driver;
public Button (WebDriver driver) {
this.driver = driver;
}
#FindBy(name="btnG")
WebElement button;
public void click() {
button.click();
}
public <T> T get(Class<T> expectedPage){
return PageFactory.initElements(driver, expectedPage);
}
}
Class of HomePage
public class HomePage {
WebDriver driver;
public HomePage(WebDriver driver) {
this.driver = driver;
}
#FindBy(xpath="//div[#class='widget']//a[text()='']")
WebElement linkInWidget;
public void click() {
linkInWidget.click();
}
public <T> T get(Class<T> expectedPage, String name){
return PageFactory.initElements(driver, expectedPage);
}
}
My test
public class searchTest {
WebDriver driver;
#BeforeTest
public void setup() {
driver = new FirefoxDriver();
driver.get("https://www.google.com.ua/");
}
#Test
public void testUI() {
HomePage homePage = PageFactory.initElements(driver, HomePage.class);
widget widget = PageFactory.initElements(driver, widget.class);
homePage.get(widget.class).get(Input.class).setValue("yahoo");
homePage.get(widget.class).get(Button.class).click();
}
}
And a result is that we can compose any object by using our classes
homePage.get(widget.class).get(Input.class).setValue("yahoo");
But how to get element by it name or number for example:
homePage.get(widget.class, "Name").get(Input.class, 1).setValue("yahoo");
I have a public repository here where I have implemented PageObject and PageFactory concept with TestNG. You are probably looking for a better way to inherit BaseClasse. The common methods should be placed in BaseClass and available to all PageObjects through inheritance. I have everything placed in GitHub and it's too broad to implement here.

Resources