nightwatch icon indicating copy to clipboard operation
nightwatch copied to clipboard

Using .url() on page object results in "expression is not callable, Type 'String' has no call signatures." typescript error

Open reallymello opened this issue 1 year ago • 13 comments

Description of the bug/issue

When I use myPageObject.url() I expect the string generated/returned by the url function in my page object to be returned, but instead I receive a TypeScript error "expression is not callable, Type 'String' has no call signatures." though it does return the string.

I think the types need to be updated to reflect that .url can be a string or a function type. I think calling .url should work for when the url property in the page object is a string and .url() should return the result of the url parameter when it is defined as a function.

image

image

Steps to reproduce

const myPageObject: PageObjectModel = {
  url: function(this: myPageObject) {
    return `https://${baseUrl}/index.aspx`;
  }
}

Sample test

const myPageObject = browser.page.myPageObject();
console.log(myPageObject.url());

Command to run

npx nightwatch

Verbose Output

n/a TypeScript error in IDE only

Nightwatch Configuration

n/a TypeScript error in IDE only

Nightwatch.js Version

3.1.3

Node Version

18.5

Browser

Chrome 115

Operating System

Windows 11

Additional Information

No response

reallymello avatar Aug 31 '23 18:08 reallymello

I will give this a try

Tanush-J avatar Oct 05 '23 13:10 Tanush-J

Hi @AutomatedTester ! I would love to take up this task. Can you please assign it to me? I would love to start my opensource journey here.

NithinSravan avatar Oct 10 '23 06:10 NithinSravan

@NithinSravan Sure go ahead.

gravityvi avatar Oct 11 '23 08:10 gravityvi

Hey, will assign once we have a PR up for this. That way we prevent issues being blocked On 11 Oct 2023 at 09:15 +0100, Ravi Sawlani @.***>, wrote:

@NithinSravan Sure go ahead. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

AutomatedTester avatar Oct 13 '23 14:10 AutomatedTester

To reproduce this issue, clone https://github.com/garg3133/nightwatch-typescript-boilerplate repository (nightwatch-v3 branch) and then inside test/dropdown.ts, add console.log(dropDownPage.url()) anywhere inside the it block.

garg3133 avatar Feb 22 '24 06:02 garg3133

Working on it @garg3133

Gmin2 avatar Feb 24 '24 08:02 Gmin2

Sure @Min2who, you can start with investigating this issue and suggest a potential solution.

garg3133 avatar Feb 24 '24 09:02 garg3133

Hello @garg3133, I have been working on this issue since yesterday, I tried to reproduce the issue as you mentioned above comment. I cloned the repo and initially while running the test using npm run test, I faced a Chromedriver version mismatch error.

error: 'session not created', message: 'session not created: This version of ChromeDriver only supports Chrome version 103\n'

I fixed the issue by upgrading the Chromedriver to the latest version using npm install chromedriver@latest --save-dev. This resolved the error and allowed me to run the tests successfully.

Following your instructions, I added console.log(dropDownPage.url()) to src/dropdown.ts. However, I encountered a name mismatch error, which I resolved by changing the name to console.log(dropdown.url());. Despite this, I did not encounter any TypeScript errors. Also, I am seeing a difference, the owner of the issue is using myPageObject type, and https://github.com/garg3133/nightwatch-typescript-boilerplate this repo is using EnhancedPageObject type. I also tried to import the myPageObject from the nightwatch in the same repo but it says module not found. Is there any step that I have missed while reproducing the issue? image

piyushmishra1416 avatar Feb 24 '24 11:02 piyushmishra1416

clone the nightwatch-v3 branch

Gmin2 avatar Feb 24 '24 11:02 Gmin2

Hey @garg3133

export type EnhancedPageObject<
  Commands = {},
  Elements = {},
  Sections extends Record<string, PageObjectSection> = {},
  Props = {},
  URL = string
>

changing the type of URL in in page-object.d.ts will solve this problem

export type EnhancedPageObject<
  Commands = {},
  Elements = {},
  Sections extends Record<string, PageObjectSection> = {},
  Props = {},
  URL = string | (() => string)
>

should i proceed with making a pr for this

Gmin2 avatar Feb 24 '24 11:02 Gmin2

I am now able to reproduce the issue. Thanks @Min2who for providing the fix. I think the actual type for the URL will be URL = string | ((...args: any) => string) but not sure. Can you please confirm on this @garg3133 ?

piyushmishra1416 avatar Feb 24 '24 12:02 piyushmishra1416

Hello @garg3133, could you assign this issue to me if the solution provided in comment is viable?

piyushmishra1416 avatar Feb 26 '24 15:02 piyushmishra1416

Could you please assign this issue to me, @garg3133?

nikhil-babar avatar Jul 19 '24 13:07 nikhil-babar