0

I am using a page object model. So each page.js file contains a variable url and 'AddButtonId'. I call the function to launch the page by passing the pageName and elementID.

page.js

module.exports = {
    e: {
        url: 'http://localhost:4200/',
        AddButtonId: 'addButton',

},
};

function definition:

urlCall: function (pageName) {
          return  browser.get(pageName+".e.url");
        },

clickButton: function(pageName,elementID){
             var x = pageName+".e."+elementID;
             element(by.id(x)).click();
            },

here I pass pageName and ID while calling the function.

    urlcall(page);

    clickButton(page, AddButtonId);

But instead of getting the variable value (http://localhost:4200/) from the page object file, the browser.get() tries to load "pageName.e.url". Instead of passing "addButton" in "clickButton" function, it passes "page.e.AddButtonId" and fails with the error " NoSuchElementError: No element found using locator: By(css selector, *[id="page.e.AddButtonId"])"

7
  • 1
    Try changing pageName+".e.url" to pageName.e.url Commented Mar 29, 2018 at 9:05
  • I am passing pageName in the function. I need to concatinate "pageName" and ".e.url", so that I can call the variable. Commented Mar 29, 2018 at 9:45
  • Did you mean concatenate pageName and e.url rather than ".e.url" Commented Mar 29, 2018 at 9:57
  • pageName.e.url is the varible I want to call. I pass the pageName as parameter in the function. Then I need to concatinate "pageName" and ".e.url" Commented Mar 29, 2018 at 10:20
  • So pageName is passed in to the function and it has a property e, and e has a property called url? Commented Mar 29, 2018 at 10:22

2 Answers 2

1

I think you need to import the page object by using the passed-in pageName to get the import file path.

// pageA.js

module.exports = {
    e: {
        url: 'http://localhost:4200/',
    },
    ...
};

// function: urlCall

urlCall: function (pageName) {
  // you need to import the page object.
  var page = require(pageName+'.js');
  var x = page.e.url;
  browser.get(x);
},

// function clickButton

clickButton: function(pageName,elementID){
    var page = require(pageName+'.js');
    var x = page.e[elementID];
    element(by.id(x)).click();
},

// test.js

urlCall('pageA')
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. This works for "urlCall" function. But i faced similar issue while passing pageName and variableName in another function. I have updated the description. please have a look at the "clickButton" function and suggest a solution
Update the code for clickButton in answer. Anyway your code implement/design is not a good pattern.
Thanks. The implement/design is so because I am using Cucumber-PageObjectModel combination. I am writing Tests in Gherkin which would have the pageName and elementID. The step definitions for the same will call the functions. So I am writing reusable steps where anyone can pass pageName and elementID to perform functions on any page
Can anyone give me a java equivalent to this answer.
0

Javascript resolves your expression to a string. Instead, you need to use square brackets like pageName["e"]["url"] or proper property access: pageName.e.url

Comments

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.