0

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

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

它显示一个错误,即“org.openqa.selenium.JavascriptException: javascript error: Cannot read property 'setAttribute' of null”。

任何人都可以帮助解决这个问题。

我的功能文件方案是:-

        @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”。它表示它无法读取任何属性。此处未定义任何属性。
请帮我解决这个问题。无法找到此问题的根本原因。

4

1 回答 1

0

试试这个来突出元素,

js.executeScript("arguments[0].setAttribute('style', 'color: blue; border: 2px solid Magenta;');", last_name); 
于 2019-07-09T11:49:55.883 回答