Hi,
I am working on serenity -cucumber projects with testrunner class, stepdefinition class , and individual page object abstraction is mentioned here.
I have also mentioned in serenity.properties file
serenity.properties:
webdriver.driver=CHROME
webdriver.chrome.driver=C://Cucumber//chromedriver_win32//chromedriver.exe
serenity.use.unique.browser=true
My issue is that I am combining 3 feature file with filtering tags to create a testcase .But chrome driver is invoking 3 times without doing anything and consequently tests are failing.
testrunner class: (Directory :test.resources.run)
package test.resources.run;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
import org.openqa.selenium.WebDriver;
import net.serenitybdd.cucumber.CucumberWithSerenity;
import net.serenitybdd.junit.runners.SerenityRunner;
import net.thucydides.core.annotations.Managed;
import net.thucydides.core.annotations.ManagedPages;
import net.thucydides.core.annotations.Steps;
import net.thucydides.core.pages.Pages;
import net.serenitybdd.cucumber.SerenityReporter;
@RunWith(CucumberWithSerenity.class)
@CucumberOptions(
features = "src/test/resources/features"
,glue={"test/resources/steps"}
,tags={"@Login,@ProfileCheck,@Logout"}
public class TestRunner {
}
I have 3 feature files namely alogin.feature,bdpacheck.feature and clogout.feature
Directory(test.resources.features)
alogin.feature
Feature: Login Action
@Login
Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User enters UserName and Password
|Fields | Value|
|username | UserID|
|pwd| Pwd|
Then Message displayed Login Successfully
bdpacheck.feature
Feature: DPA Check in HomePage
@ProfileCheck
Scenario:Customer Profile Check
Given the agent has performed search for customer "Cust Name" in HomePage
And the search result returns a list of customer
When the agent has selected the customer "Mr Cust Name"
Then the agent can view corresponding customer profiles within customer
clogout.feature
Feature: Logout action
@Logout
Scenario: Successful LogOut
When User LogOut from the Application
Then Message displayed LogOut Successfully
StepDefinition file
Directory (test.resources.steps)
TestStpes.java
package test.resources.steps;
import java.util.concurrent.TimeUnit;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.apache.log4j.LogManager;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;
//import org.testng.annotations.BeforeTest;
import cucumber.api.DataTable;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import net.thucydides.core.annotations.Step;
import test.resource.connexcase.config.Config;
import test.resource.connexcase.steps.*;
import net.serenitybdd.junit.runners.SerenityRunner;
import net.thucydides.core.annotations.Managed;
import net.thucydides.core.annotations.ManagedPages;
import net.thucydides.core.annotations.Steps;
import net.thucydides.core.pages.Pages;
public class TestSteps {
@Steps
LoginSteps loginHome;
@Steps
CustomerCheckSteps custCheck;
@Steps
LogoutSteps logout;
@Given("^User is on Home Page$")
public void OpenBaseUrl()
{
String str = "salesforce";
loginHome.openURL(str);
}
@When("^User enters UserName and Password$")
public void LoginwithCred(DataTable table) throws InterruptedException
{
// WHEN User enters UserName and Password
loginHome.LoginWithValidCredential(table);;
}
@Then("^Message displayed Login Successfully$")
public void Loginhome()
{
String str = "Case - Console";
// THEN Message displayed Login Successfully
loginHome.NaviagteHomePage(str);
}
@Given("^the agent has performed search for customer \"([^\"]*)\" in Connex Case$")
public void SearchCustomer(String CustName) throws InterruptedException
{
custCheck.custSearch(CustName);
}
@Given("^the search result returns a list of customer$")
public void ListOfCustomer()
{
custCheck.custList();
}
@When("^the agent has selected the customer \"([^\"]*)\"$")
public void SelectCustomer(String Customer) throws InterruptedException
{
custCheck.selectCust(Customer);
}
@Then("^the agent can view corresponding customer profiles within customer$")
public void CustomerView()
{
custCheck.CustProfileview();
}
@When("^User LogOut from the Application$")
public void LogOff() throws InterruptedException
{
logout.LoggingOff();
}
@Then("^Message displayed LogOut Successfully$")
public void LogOffPage()
{
logout.LandingPage();
}
}
and the corresponding step library are LoginSteps.java,CustomerCheckSteps.java and LogoutSteps.java
are placed under test.resource.connexcase.steps
LoginSteps.java,
package test.resource.connexcase.steps;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import cucumber.api.DataTable;
import test.resource.connexcase.config.Config;
import test.resource.connexcase.pages.HomePage;
import net.thucydides.core.annotations.Step;
public class LoginSteps {
HomePage homepage;
@Step
public void openURL(String string1) {
// TODO Auto-generated method stub
homepage.open1(string1);
}
@Step
public void LoginWithValidCredential(DataTable table) throws InterruptedException {
// TODO Auto-generated method stub
homepage.LoginWithValidCredential(table);
}
@Step
public void NaviagteHomePage(String string4) {
// TODO Auto-generated method stub
homepage.NaviagteHomePage(string4);
}
}
CustomerCheckSteps.java
package test.resource.connexcase.steps;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import test.resource.connexcase.config.Config;
import test.resource.connexcase.pages.SearchAndCustprofileCheck;
import net.thucydides.core.annotations.Step;
public class CustomerCheckSteps {
SearchAndCustprofileCheck custdetail;
@Step
public void custSearch(String string1) throws InterruptedException {
// TODO Auto-generated method stub
custdetail.customerSearch(string1);
}
@Step
public void custList() {
// TODO Auto-generated method stub
custdetail.customerList();
}
@Step
public void selectCust(String string2) throws InterruptedException {
// TODO Auto-generated method stub
custdetail.customerSelect(string2);
}
@Step
public void CustProfileview() {
// TODO Auto-generated method stub
custdetail.custProfile();
}
}
LogoutSteps.java
package test.resource.connexcase.steps;
import org.junit.Test;
import test.resource.connexcase.pages.LogoutApp;
import net.thucydides.core.annotations.Step;
public class LogoutSteps {
LogoutApp logoutapp;
@Step
public void LoggingOff() throws InterruptedException
{
logoutapp.logout();
}
@Step
public void LandingPage()
{
logoutapp.landingPage();
}
}
The pgaeObject classed are mentioned below for each of Login(HomePage.java),CustomerCheck(SearchAndCustprofileCheck.java) and Logout(LogoutApp.java)
HomePage.java
package test.resource.connexcase.pages;
import test.resource.connexcase.config.Config;
import test.resources.steps.TestSteps;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
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.pagefactory.*;
import cucumber.api.DataTable;
import net.serenitybdd.core.pages.PageObject;
import net.serenitybdd.core.pages.WebElementFacade;
import net.thucydides.core.annotations.DefaultUrl;
import net.thucydides.core.annotations.Managed;
import net.thucydides.core.annotations.ManagedPages;
import net.thucydides.core.pages.Pages;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Base64;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class HomePage extends PageObject{
Logger Log = LogManager.getLogger("DebugLogger");
public void open1(String logo)
{
//getDriver().get("Url");
String txt = getDriver().findElement(By.xpath("//img[@id='logo']")).getText();
System.out.println(txt);
if (txt ==logo)
{
Log.info("User Is on home");
//System.out.println("User is on home Page");
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void LoginWithValidCredential(DataTable table) throws InterruptedException
{
List<List<String>> data = table.raw();
System.out.println(data.get(1).get(1));
getDriver().findElement(By.id("username")).sendKeys(data.get(1).get(1));
// Decryption of string
System.out.println(data.get(2).get(1));
String encryptedPassword = data.get(2).get(1);
byte[] decryptedPasswordBytes = Base64.getDecoder().decode(encryptedPassword);
String decryptedPassword = new String(decryptedPasswordBytes);
getDriver().findElement(By.id("password")).sendKeys(decryptedPassword);
//driver.findElement(By.id("username")).sendKeys(usrID);
// driver.findElement(By.id("password")).sendKeys(Pwd);
getDriver().findElement(By.id("Login")).click();
Thread.sleep(2);
}
public void NaviagteHomePage(String browserTitle) {
// TODO Auto-generated method stub
getDriver().manage().window().maximize();
try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Boolean bb = getDriver().getTitle().equalsIgnoreCase(browserTitle);
if (bb)
{
Log.info("Login successfully in the Salesforce Portal");
//System.out.println("Login Successfully");
}
}
}
SearchAndCustprofileCheck.java
package test.resource.connexcase.pages;
import test.resources.steps.TestSteps;
import test.resource.connexcase.config.Config;
import test.resource.connexcase.pages.HomePage;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import net.serenitybdd.core.pages.PageObject;
import net.serenitybdd.core.*;
import net.serenitybdd.core.pages.WebElementFacade;
import net.thucydides.core.annotations.DefaultUrl;
import net.thucydides.core.annotations.Managed;
import net.thucydides.core.guice.Injectors;
import net.thucydides.core.webdriver.WebdriverManager;
import java.util.Base64;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class SearchAndCustprofileCheck extends PageObject
{
Logger Log = LogManager.getLogger("DebugLogger");
public void customerSearch(String cust) throws InterruptedException
{
//Injectors.getInjector().getInstance(WebdriverManager.class).getWebdriver();
Thread.sleep(4);
getDriver().findElement(By.xpath("//input[@id='phSearchInput']")).clear();
getDriver().findElement(By.xpath("//input[@id='phSearchInput']")).sendKeys(cust);
getDriver().findElement(By.xpath("//input[@id='phSearchInput']")).sendKeys(Keys.ENTER);
Thread.sleep(2);
}
public void customerList()
{
getDriver().switchTo().frame("ext-comp-1019");
if(getDriver().findElement(By.id("Contact_body")).isDisplayed())
{
Log.info("Search Returns the Specific Customer");
//System.out.println("Search return the specified customer");
}
}
public void customerSelect(String cust) throws InterruptedException
{
java.util.List<WebElement> links = getDriver().findElements(By.tagName("a"));
for (int i = 1; i<=links.size()-1; i=i+1)
{
//System.out.println(links.get(i).getText());
if (links.get(i).getText().equalsIgnoreCase(cust))
{
links.get(i).click();
System.out.println("Clicked");
Thread.sleep(2);
}
}
}
public void custProfile()
{
WebElement aa = getDriver().findElement(By.className("pbHeader"));
WebElement bb =aa.findElement(By.className("pbTitle"));
String hh = bb.getText().toString();
if (hh.contains("Customer Detail"))
{
Log.info("Agent is able to view the customer details");
//System.out.println("Agent able to view the customer details");
}
getDriver().switchTo().defaultContent();
}
}
LogoutApp.java
package test.resource.connexcase.pages;
import test.resources.steps.TestSteps;
import test.resource.connexcase.config.Config;
import test.resource.connexcase.pages.HomePage;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import net.serenitybdd.core.pages.PageObject;
import net.serenitybdd.core.pages.WebElementFacade;
import net.thucydides.core.annotations.DefaultUrl;
import net.thucydides.core.annotations.Managed;
import java.util.Base64;
import java.util.concurrent.TimeUnit;
public class LogoutApp extends PageObject{
//static WebDriver driver;
Logger Log = LogManager.getLogger("DebugLogger");
public void logout() throws InterruptedException
{
getDriver().findElement(By.xpath("//span[@id ='userNavLabel']")).click();
Thread.sleep(2);
getDriver().findElement(By.xpath("//a[@title='Logout']")).click();
Thread.sleep(2);
}
public void landingPage()
{
Log.info("Logged out successfully");
boolean cc = getDriver().getTitle().equalsIgnoreCase("Login | Salesforce");
//System.out.println("Logout Successfully");
if (cc)
{
Log.info("Logged out successfully");
}
getDriver().quit();
}
}
Can you please help me in this regards?As each time I tried to execute testrunner.class as junit testcases , it basically trying to open 3 blank chrome page but without doing nothing although defined all properties in serenity.properties along with default.url. I want entire flow to be executed in a single chrome driver session instead of opening 3 chrome browser.
Please help me to resolve this.
Regards,
Avijit Dutta