0

New to Cucumber, executing it along with Selenium. I have written a feature file as where user first goes to a home page then clicks on Register link & verify that the said fields are displayed.:-

The step definition for the same has also been written. But when i execute the feature file .

It shows an error i.e " org.openqa.selenium.JavascriptException: javascript error: Cannot read property 'setAttribute' of null" .

Could anyone help out to solve this issue.

My Feature file scenario is :-

        @TC2
        Scenario: Verify the Register link provided on Home Page

           Given user is on  HomePage
           When user clicks on register link
           Then user should be able to view following <fields> such as

           |fields|
           |First Name|
           |Last Name|
           |Address|
           |City|
           |State|
           |Zip code|
           |Phone|
           |SSN|
           |Username|
           |Password|
           |Confirm| 

Step Definition File is as follows :=

 @When ("^user clicks on register link$")
            public void click_register()
            {

                 WebElement Register_link=driver.findElement(By.xpath("//a[contains(text(),'Register')]"));
                 Register_link.click();
            }


            @FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.firstName']")
            private WebElement first_name;

            @FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.lastName']")
            private WebElement last_name;

            @FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.address.street']")
            private WebElement Address;

            @FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.address.city']")
            private WebElement city;

            @FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.address.state']")
            private WebElement state;

            @FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.address.zipCode']")
            private WebElement zip;

            @FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.phoneNumber']")
            private WebElement phone;

            @FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.ssn']")
            private WebElement SSN;

            @FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.username']")
            private WebElement Username;

            @FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='customer.password']")
            private WebElement password;

            @FindBy(xpath="//table[@class='form2']/tbody/tr/td[2]/input[@id='repeatedPassword']")
            private WebElement repeat_pass;

            @Then ("^user should be able to view  <fields> such as$")
            public void verify_fields(DataTable testData)
            {
                List<String> all_fields=testData.asList(String.class);  

                for(String fields_links:all_fields)
                {

                    JavascriptExecutor js=(JavascriptExecutor)driver;
                    js.executeScript("arguments[0].setAttribute('style', arguments[1]);",first_name, "color: blue; border: 2px solid Magenta;");



                    js.executeScript("arguments[0].setAttribute('style', arguments[1]);",last_name, "color: blue; border: 2px solid Magenta;");


                    js.executeScript("arguments[0].setAttribute('style', arguments[1]);",Address, "color: blue; border: 2px solid Magenta;");

                    js.executeScript("arguments[0].setAttribute('style', arguments[1]);",city, "color: blue; border: 2px solid Magenta;");

js.executeScript("arguments[0].setAttribute('style', arguments[1]);",state, "color: blue; border: 2px solid Magenta;");


                    js.executeScript("arguments[0].setAttribute('style', arguments[1]);",zip, "color: blue; border: 2px solid Magenta;");


                    js.executeScript("arguments[0].setAttribute('style', arguments[1]);",phone, "color: blue; border: 2px solid Magenta;");



                    js.executeScript("arguments[0].setAttribute('style', arguments[1]);",SSN, "color: blue; border: 2px solid Magenta;");


                    js.executeScript("arguments[0].setAttribute('style', arguments[1]);",phone, "color: blue; border: 2px solid Magenta;");



                    js.executeScript("arguments[0].setAttribute('style', arguments[1]);",Username, "color: blue; border: 2px solid Magenta;");


                    js.executeScript("arguments[0].setAttribute('style', arguments[1]);",password, "color: blue; border: 2px solid Magenta;");


                    js.executeScript("arguments[0].setAttribute('style', arguments[1]);",repeat_pass, "color: blue; border: 2px solid Magenta;");

                }

JavascriptExecutor is used here to highlight the elements in order to verify it exist. This is the error message - "org.openqa.selenium.JavascriptException: javascript error: Cannot read property 'setAttribute' of null". It is stating that it cannot read any property. No property has been defined here.
Please help me out to solve this query . Unable to find the root cause of this issue.

Sureshmani Kalirajan
  • 1,938
  • 2
  • 9
  • 18
neha sharma
  • 117
  • 1
  • 3
  • 11
  • can you post the html source here ? try this for highlighting the elements,js.executeScript("arguments[0].setAttribute('style', 'color: blue; border: 2px solid Magenta;');", last_name); – Sureshmani Kalirajan Jul 09 '19 at 11:46

1 Answers1

0

try this for highlighting the elements,

js.executeScript("arguments[0].setAttribute('style', 'color: blue; border: 2px solid Magenta;');", last_name); 
Sureshmani Kalirajan
  • 1,938
  • 2
  • 9
  • 18
  • not working . The same error could be seen again i.e "org.openqa.selenium.JavascriptException: javascript error: Cannot read property 'setAttribute' of null " – neha sharma Jul 09 '19 at 12:30
  • @nehasharma please take a look at the solution here - similar issue/error - https://stackoverflow.com/questions/20506663/selenium-javascript-executor-returning-null-when-js-works-fine-on-chrome-console – Sureshmani Kalirajan Jul 09 '19 at 21:24
  • It is likely that document was still loading and js executor is trying to execute and running into this issue. you should add proper wait conditions. – Sureshmani Kalirajan Jul 09 '19 at 21:25