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.

1
  • 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); Commented Jul 9, 2019 at 11:46

1 Answer 1

0

try this for highlighting the elements,

js.executeScript("arguments[0].setAttribute('style', 'color: blue; border: 2px solid Magenta;');", last_name); 
Sign up to request clarification or add additional context in comments.

3 Comments

not working . The same error could be seen again i.e "org.openqa.selenium.JavascriptException: javascript error: Cannot read property 'setAttribute' of null "
@nehasharma please take a look at the solution here - similar issue/error - stackoverflow.com/questions/20506663/…
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.