nightwatchjs / nightwatch

Integrated end-to-end testing framework written in Node.js and using W3C Webdriver API. Developed at @browserstack
https://nightwatchjs.org
MIT License
11.78k stars 1.31k forks source link

Error on asserting .getElementAttribute() protocol action on IOS Simulator #3568

Open richardjimenez52 opened 1 year ago

richardjimenez52 commented 1 year ago

Description of the bug/issue

When running nightwatch tests against an ios simulator I get failures when trying to the following command' browser.expect.element(inputElement).to.have.attribute('value').to.equal('Item 1');

The IOS simulator is from the package nightwatchjs/mobile-helper-tool

I'm running tests against a local enviornment on port 4200 yet the error I get when it gets to my assertions is: Error while running .getElementAttribute() protocol action: Error ECONNREFUSED: connect ECONNREFUSED 127.0.0.1:80

The browser steps before that test such as .click, .waitForElementPresent work as expected. If i pause the tests and inspect the element it indeed has the correct value i'm trying to assert yet it fails on ios simulator but passes when being ran against regular desktop browser.

Screen Shot 2023-01-12 at 3 49 31 PM

Steps to reproduce

No response

Sample test

import { NightwatchBrowser, NightwatchTests } from 'nightwatch';

const autocompleteTests: NightwatchTests = {
  "@tags": ["autocomplete"],
  beforeEach: (browser: NightwatchBrowser) => {
    browser.url(browser.launch_url + '/autocomplete');
  },
  afterEach: (browser: NightwatchBrowser) => {
    if(browser.globals.env == 'smoke'){
      browser.endSauce();
    }

    browser.end();
  },
  "should have initial and search results": async (browser: NightwatchBrowser) => {
    const auto = element('#auto');
    const item1 = await element('#ukg-menu-item-1');
    const inputElement = element('#input');

    browser
      .waitForElementPresent(auto)
      .waitForElementVisible(auto)
      .click(auto);

    browser
      .waitForElementPresent(item1)
      .waitForElementVisible(item1)
      .click(item1);

    browser
      .expect.element(inputElement).to.have.attribute('value').to.equal('Item 1');
  }
}

export default autocompleteTests;

Command to run

"test:e2e-smoke": "npm run build:e2e-smoke && nightwatch --config ./packages/wc-ng-tests-e2e/smoke/nightwatch/nightwatch.conf.js --retries 2 --reporter junit --reporter html --env ios.simulator.safari --tag autocomplete",

Verbose Output

Error
      GET  /session/145AE814-6169-4520-B369-8492659CC250/element/node-311CB350-FF50-4AEB-AEBF-C1C7F72D9B54/attribute/value - ECONNREFUSED
Error: connect ECONNREFUSED 127.0.0.1:80
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)
    Error   Error while running .getElementAttribute() protocol action: Error ECONNREFUSED: connect ECONNREFUSED 127.0.0.1:80

Nightwatch Configuration

'ios.simulator.safari': {
      launch_url: "http://localhost:4200/smoke",
      skip_testcases_on_fail: false,
      desiredCapabilities: {
        browserName: 'safari',
        platformName: 'iOS',
        'safari:useSimulator': true,
        // change the deviceName, platformVersion accordingly to run tests on
        // Run command: `xcrun simctl list devices`
        // 'safari:platformVersion': '15.0',
        'safari:deviceName': 'iPhone 8'
      },

      webdriver: {
        start_process: true,
        server_path: '',
        cli_args: [
          // --verbose
        ]
      }
    },

Nightwatch.js Version

^2.6.10

Node Version

v16.13.2

Browser

safari

Operating System

iphone 8

Additional Information

the ios simulator is from @nightwatch/mobile-helper": "^0.1.8"

harshit-bs commented 1 year ago

Hey @richardjimenez52 It throws an error because Nightwatch communicates directly with WebDrivers in the case of elemet expect assertion commands, which requests required a port that was never assigned. It is because safaridriver's default port value is 0 and 0 is considered false by Javascript in if statements.

So can you try the following configuration ?

'ios.simulator.safari': {
      launch_url: "http://localhost:4200/smoke",
      skip_testcases_on_fail: false,
      desiredCapabilities: {
        browserName: 'safari',
        platformName: 'iOS',
        'safari:useSimulator': true,
        // change the deviceName, platformVersion accordingly to run tests on
        // Run command: `xcrun simctl list devices`
        // 'safari:platformVersion': '15.0',
        'safari:deviceName': 'iPhone 8'
      },

      webdriver: {
        start_process: true,
        server_path: '',
        host: '127.0.0.1',
        port: 4444,
        cli_args: [
          // very verbose geckodriver logs
          // '-vv'
        ]
      }
    },
richardjimenez52 commented 1 year ago

Hi @harshit-bs , after adding those changes to the webdriver object, now the simulator doesn't even spin up the device. I am manually able to spin up the device by running open -a simulator but not through nightwatch. I added --verbose and here is the log

this is my new configuration:

    'ios.simulator.safari': {
      globals:{
        env: "ios"
      },
      launch_url: "http://localhost:4200/smoke",
      skip_testcases_on_fail: false,
      desiredCapabilities: {
        javascriptEnabled: true,
        acceptInsecureCerts: true,
        real_mobile: false,
        browserName: 'safari',
        platformName: 'iOS',
        //platformVersion: '15.0',
        'safari:useSimulator': true,
        // change the deviceName, platformVersion accordingly to run tests on
        // Run command: `xcrun simctl list devices`
        // 'safari:platformVersion': '15.0',
        'safari:deviceName': 'iPhone X'
      },

      webdriver: {
        start_process: true,
        host: '127.0.0.1',
        port: 4444,
        server_path: '',
        cli_args: [
          // --verbose
        ]
      }
    },

Screen Shot 2023-02-02 at 1 08 55 PM

harshit-bs commented 1 year ago

It seems strange. Perhaps we're overlooking something. I have created a sample repository incorporating the information you shared. Would you mind testing it and letting me know the outcome?

richardjimenez52 commented 1 year ago

Hi @harshit-bs apologies for the late reply. I can't seem to be able to run test example on the repo you created. I'm getting an error with safari driver? Screen Shot 2023-02-23 at 3 44 52 PM