0

I have certain list of elements that i want to highlight it. I am using POM as a design pattern. Using cucumber with Java. I am using DataTable to fetch list of records. Please guide me in the code part that i have written.

    *My Feature File* :-
    @TC1
    Scenario: Verify the Dashboard of the Website / Home page of the website
        Given user is on  HomePage
        Then user should be able to view following <links> such as 

         |links|
         |Solutions|
         |About Us |
         |Services |
         |Products |
         |Locations|
         |Home|
         |About Us|
         |Contact Us|
         |ATM Services|
         |Online Services|
         |Register|
         |Forgot link info|

I want to highlight these elements shown under links header.

How to use List as a parameter & highlight the elements? I want to define my function in homePage.java & call it under Step Definition file.

In 'src/main/java' , i have made a java class file named as "HomePage.java" where in i have defined my elements using @FindBy keyword & added some methods performing some functions .

View of my HomePage.Java

public class HomePage {

    public static WebDriver driver;

    /*public HomePage(WebDriver driver)
    {
        PageFactory.initElements(driver, this);
    }*/


    @FindBy(xpath="html/body/div[1]/div[2]/ul/li/a")
    private List<WebElement> links;


public void user_homePage()
    {
        System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.get("https://parabank.parasoft.com/parabank/index.htm");
        //driver.manage().window().maximize();
    }

    public void highlight_On_Element(DataTable dt) throws Throwable
    {
          List<String> data=dt.asList(String.class);

          {

          driver.findElement(By.name(data.get(0)));
          JavascriptExecutor js=(JavascriptExecutor) driver;
          js.executeScript("arguments[0].setAttribute('style', arguments[1]);",data, "color: blue; border: 2px solid Magenta;");
          }

In 'src/test/java', I have made a step definitions file & Test Runner class.

So view of my step definition file is:

package StepDefinitions;

import java.util.List;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import cucumber.api.DataTable;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import PageObjects.HomePage;


public class Steps extends HomePage
{
     private HomePage home;



    @Given ("^user is on  HomePage$")
    public void home_page()
    {
        HomePage home=new HomePage();
        home.user_homePage();
    }


   @Then ("^user should be able to view following <links> such as$")
    public void verify_links(DataTable dt) throws Throwable
    { 
      // List<String> data=dt.asList(String.class);

      home.highlight_On_Element(dt); // Calling func "highlight_on _element define in Home page java file.
     } 


}

Error is displayed when i execute either feature file / Test runner java file , the error is

java.lang.NullPointerException

the error is highlighting on step

home.highlight_On_Element(dt);

defined in step definition file .

i am new to learning cucumber by myself.

Expected Result:

I want to highlight the elements "Links" written in feature file. I have written the xpath's of following links in a form of List.

How can one use List as a parameter in a function & calling it in a step definition file . Using List i can traverse each & every element defined under header "Links" & highlight it using JavaScriptExecutor.

James Z
  • 12,209
  • 10
  • 24
  • 44

1 Answers1

0

If your findElement By method is working and finding the element properly, you can highlight every field with following code block :

public void highlight_On_Element(DataTable dt) throws Throwable
    {
          List<String> data=dt.asList(String.class);

      for(int i=0; i<data.size(); i++) {
      driver.findElement(By.name(data.get(i)));
      JavascriptExecutor js=(JavascriptExecutor) driver;
      js.executeScript("arguments[0].setAttribute('style', arguments[1]);",data, "color: blue; border: 2px solid Magenta;");
      }
}

And in your Step Definition file you can use it as following :

   @Then ("^user should be able to view following links such as$")
    public void verify_links(DataTable dt) throws Throwable
    { 
      home.highlight_On_Element(dt);
     } 

Or you can convert that dataTable to list in your step definition file and you can pass that list as a parameter however before do it you must change the parameter for highlight_On_Element method from DataTable to List.

public void highlight_On_Element(List lst) throws Throwable
        {
          for(int i=0; i<lst.size(); i++) {
          driver.findElement(By.name(lst.get(i)));
          JavascriptExecutor js=(JavascriptExecutor) driver;
          js.executeScript("arguments[0].setAttribute('style', arguments[1]);",data, "color: blue; border: 2px solid Magenta;");
          }
    }
sayhan
  • 1,168
  • 1
  • 16
  • 22
  • Thanks for letting me know , I have one question in this . If i need to call this method in a Step definition file. how will i define the parameter in calling method . Eg. home.highlight_On_Element(DataTable dt) is correct or not . Because mainly the error "Null pointer Exception" is shown in calling method – neha sharma Jul 26 '19 at 09:28
  • @nehasharma here is a explanation http://www.automationtestinghub.com/cucumber-data-table/ – sayhan Jul 26 '19 at 09:32
  • @nehasharma and last answer here : https://stackoverflow.com/questions/46130455/how-to-convert-a-datatable-in-cucumber-to-a-list-of-objects – sayhan Jul 26 '19 at 09:34
  • I called the function as you have written on it . But it shows an error "Null Pointer Exception" on execution of Test Runner class – neha sharma Jul 26 '19 at 09:55
  • In step definition i called the function as :- @Then ("^user should be able to view following such as$") public void verify_links(DataTable dt) throws Throwable { home.highlight_On_Element(dt); } it is throwing an error of 'Null pointer Exception' – neha sharma Jul 26 '19 at 09:58
  • dont use <> this on just use links @nehasharma – sayhan Jul 26 '19 at 10:01
  • An error is shown "You can implement missing steps with the snippets below: @Then("^user should be able to view following such as$") public void user_should_be_able_to_view_following_links_such_as(DataTable arg1) throws Throwable { // Write code here that turns the phrase above into concrete actions // For automatic transformation, change DataTable to one of // List, List>, List> or Map. // E,K,V must be a scalar (String, Integer, Date, enum etc) throw new PendingException(); – neha sharma Jul 26 '19 at 10:41
  • my one feature is exeuting correct , but not the other one. – neha sharma Jul 26 '19 at 11:13
  • First error throwing because you changed the step and press run again. Then the ide didn't find the step. And if solutions works please accept my answer because it is working. @nehasharma – sayhan Jul 26 '19 at 11:25
  • did not understand your answer . Could you please clear the point you are trying to say. – neha sharma Jul 26 '19 at 11:28
  • When you changed the step definition name and press run button in your IDE(intelliJ,eclipse whatever), it is not gonna find that step because you changed. this is why you get the first error message. And if my solution works for one feature file but not for other this means solutions is correct. You should refactor your other feature files. @nehasharma – sayhan Jul 26 '19 at 11:31
  • it shows me an error "no such element: Unable to locate element: {"method":"css selector","selector":"*[name='links']"} " however the xpath of an element is correct – neha sharma Aug 22 '19 at 10:10
  • @FindBy(xpath="html/body/div[1]/div[2]/ul/li/a") private List links; – neha sharma Aug 23 '19 at 09:43
  • written the following code :- @Then("^user should be able to view following such as$") public void verify_links(DataTable dt) throws Throwable { List data=dt.asList(String.class); for(int i=0;i – neha sharma Aug 23 '19 at 09:43
  • Selcuk Ayhan could you please help me out – neha sharma Sep 03 '19 at 09:34
  • @nehasharma where did you stuck? – sayhan Sep 03 '19 at 09:35