错误显示为“ org.openqa.selenium.JavascriptException:JavaScript错误:无法读取null的属性'setAttribute'

时间:2019-07-09 10:34:45

标签: selenium-webdriver cucumber bdd feature-file

Cucumber的新手,与Selenium一起执行。我已经写了一个功能文件,用户首先进入主页,然后单击“注册”链接并验证是否显示了所述字段。:-

还编写了相同的步骤定义。但是当我执行功能文件时。

显示错误,即“ org.openqa.selenium.JavascriptException:javascript错误:无法读取null的属性'setAttribute'。

任何人都可以帮忙解决此问题。

我的功能文件方案是:-

        @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| 

步骤定义文件如下:=

 @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突出显示元素,以验证其存在。   这是错误消息-“ org.openqa.selenium.JavascriptException: javascript error: Cannot read property 'setAttribute' of null”。 它表明它无法读取任何属性。此处未定义任何属性。
请帮我解决这个问题。无法找到此问题的根本原因。

1 个答案:

答案 0 :(得分:0)

尝试此操作以突出显示元素,

npm run dbs-migrate