serenity-bdd / serenity-cucumber

Cucumber integration for the Serenity BDD Reporting library
Other
78 stars 74 forks source link

Serenity Cucumber is failing by opening multiple browser instance of chrome driver #65

Closed aviju2007 closed 8 years ago

aviju2007 commented 8 years ago

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

wakaleo commented 8 years ago

This sounds like a chrome driver issue - have you checked that you have the latest version of Chromedriver?

aviju2007 commented 8 years ago

are u talking about selenium-chrome-driver jar? I am using selenium-chrome-driver-2.45.0.jar. Is it main cause for it??If so what version should I go for ..please suggest

Regards, Avijit

wakaleo commented 8 years ago

No, I’m talking about the binary that should be on your class path.


John Smart | Wakaleo Consulting | +44 7398 832273 Making smart teams collaborate better

http://johnfergusonsmart.com | reachme@johnfergusonsmart.com john.smart@wakaleo.com___

On 20 July 2016 at 8:21:51 PM, aviju2007 (notifications@github.com) wrote:

are u talking about selenium-chrome-driver jar? I am using selenium-chrome-driver-2.45.0.jar. Is it main cause for it??If so what version should I go for ..please suggest

Regards, Avijit

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/serenity-bdd/serenity-cucumber/issues/65#issuecomment-234053863, or mute the thread https://github.com/notifications/unsubscribe-auth/AADasijb-R2QGTDb3Ag3YMLMlYy2M5Alks5qXnVOgaJpZM4JQvcu .

aviju2007 commented 8 years ago

ok...it is chromeDriver_Win32 published in seleniumhq.com and updated one.

Regards, Avijit

wakaleo commented 8 years ago

selenium-chrome-driver-2.45.0.jar is quite an old version, so it may be an issue with the selenium version. Try making sure the selenium driver is the same one as the one used by serenity (the simplest way is simply to let Serenity take care of the selenium dependencies)

aviju2007 commented 8 years ago

Hi John,

When I am giving getDriver.get("url") in login page that time, it is navigated to login steps and doing necesary action and then closing the instance.Then when CustomerCheck steps is executed then again trying to open a chrome browser, and similarly for Logout steps another browser.But all this Steps are maintained in a StepDefinition Java class and serenity WebDriver should pass through in a single session testing @login ,Then @CustomerCheck@ and @logout in single unified session.This is causing the problem for me. Can you please check my code for StepRunner, Stepdefinition and PageObject files.

Also I have mentioned Serenity properties file

On Thu, Jul 21, 2016 at 1:08 AM, John Ferguson Smart < notifications@github.com> wrote:

selenium-chrome-driver-2.45.0.jar is quite an old version, so it may be an issue with the selenium version. Try making sure the selenium driver is the same one as the one used by serenity (the simplest way is simply to let Serenity take care of the selenium dependencies)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/serenity-bdd/serenity-cucumber/issues/65#issuecomment-234058439, or mute the thread https://github.com/notifications/unsubscribe-auth/ASTiuO-9kX155EubFmEEhFRncJzW7XvOks5qXnkrgaJpZM4JQvcu .

aviju2007 commented 8 years ago

Sorry , I got disconnected.

Properties files is mentioned as below: webdriver.driver=CHROME webdriver.chrome.driver=C://Cucumber//chromedriver_win32//chromedriver.exe serenity.use.unique.browser=true

Also the jars used:

selenium-remote-driver-2.45.0.jar selenium-server-2.45.0.jar selenium-support-2.45.0.jar serenity-cucumber-1.1.1.jar serenity-junit-1.0.47.jar serenity-maven-plugin-1.1.37-rc.6.jar serenity-report-resources-1.0.47.jar serenity-rest-assured-1.1.37-rc.6.jar gherkin-2.12.2.jar cucumber-core-1.2.2.jar cucumber-html-0.2.3.jar cucumber-jvm-deps-1.0.3.jar core-1.0.47.jar cobertura-2.1.1.jar cucumber-core-1.2.4.jar cucumber-java-1.2.4.jar cucumber-junit-1.2.4.jar cucumber-jvm-deps-1.0.5.jar cucumber-reporting-0.1.13.jar cucumber-reporting-2.5.0.jar cucumber-testng-1.2.4.jar gherkin-2.12.2.jar gherkin-4.0.0.jar junit-2.11.7.jar

mockito-all-2.0.2-beta.jar testng-142.1.jar

Seeking your help and suggestion for resolution

Regards, Avijit Dutta

wakaleo commented 8 years ago

You should avoid using getDriver() directly, and in particular calling quit() from inside the tests - Serenity will manage the driver lifecycle for you. There are also many conflicting versions in the list above - the simplest thing would be to study the tutorials (e.g http://testerstories.com/2016/06/java-automation-with-serenity/, http://serenity-bdd.info/#/documentation and http://johnfergusonsmart.com/presentations/serenity-bdd-tutorial-1-introduction-web-testing-serenity-bdd/), start a fresh project using the Maven archetypes, and then integrate the test logic from the above code.

aviju2007 commented 8 years ago

Hi John,

Thanks for your inputs.I am working in corporate VM and do not have privileges to create maven project as it will download third party plugin.I have tried to do so using Maven archetypes but build got failed due to lack of permission. So for an workaround I have created a Java projects and downloaded all the requisite jars from search.maven.org and then added in build path to create an error free project. I am using serenity BDD with cucumber junit and running Test Runner class where I am defining all the features, steps, tags etc to create the testcases as defined above. Also I cant see serenity reports for each of steps mentioned step definition file but cant see no aggregated reports which include charts and graphs. Even I tried to execute maven sereniy:aggregate report but its not working due network admin issues not allowing other software to be downloaded.

Can you please suggest is this the best way to implement Serenity BDD ?or do i need all the requisite permission(admin rights) to create a maven project for using the serenity features and concepts.what is the best practice to get success out of serenity cucumber. One more thing is it better to use jbehave or cucumber with serenity ??what is the pros and cons for both

Please suggest

Regards, Avijit

aviju2007 commented 8 years ago

Hi John,

As sugegsted by you..I have created a Maven archetype projecte using Serenity , Jbehave and Junit. Also writing scenarios in a story file(.story lcoation). But I have few queries...How to run specific story from TestSuite as Junit ?I am trying to run but seemed failing. Also Need to how to run specific story file from Maven Command Line.

Please help me in this regard.

Thanks and Regards, Avijit Dutta

aviju2007 commented 8 years ago

Closing the Issue now and As multiple browser issue is seemed to be resolved

wakaleo commented 8 years ago

Thanks for the update Avijit.