Search code examples
javascriptseleniumsalesforcenightwatch.jsnightwatch

Nightwatch 1.3.5, able to accept alert, but cannot get the text from it. Salesforce UI Automation


I am working on Salesforce UI Automation using Nightwatch.js

I am having an issue getting the text from an alert. I am able to trigger the alert and accept it, but not grab the text from it.

Scenario : 1- User clicks on get transactions 2- After some seconds, user gets message "Bank Account Transaction Details pulled successfully."

Alert example :

ALERT

According to official documentation, should be able to use getAlertText() Get Alert

In the code, doing browser.acceptAlert() works without a problem, but if before accepting I do something like this:

let alertText = browser.getAlertText()

And then log that text, I get this :

 NightwatchAPI {
  capabilities: {
    acceptInsecureCerts: false,
    acceptSslCerts: false,
    applicationCacheEnabled: false,
    browserConnectionEnabled: false,
    browserName: 'chrome',
    chrome: {
      chromedriverVersion: '83.0.4103.39 (ccbf011cb2d2b19b506d844400483861342c20cd-refs/branch-heads/4103@{#416})',
      userDataDir: '/var/folders/nd/79rp8mmj1pv6h6k51wtym__w0000gn/T/.com.google.Chrome.kCHhRk'
    },
    cssSelectorsEnabled: true,
    databaseEnabled: false,
    'goog:chromeOptions': { debuggerAddress: 'localhost:57780' },
    handlesAlerts: true,
    hasTouchScreen: false,
    javascriptEnabled: true,
    locationContextEnabled: true,
    mobileEmulationEnabled: false,
    nativeEvents: true,
    networkConnectionEnabled: false,
    pageLoadStrategy: 'normal',
    platform: 'Mac OS X',
    proxy: {},
    rotatable: false,
    setWindowRect: true,
    strictFileInteractability: false .....

It goes on, but was too long to share it all. I receive that instead of the text "Bank Account Transaction Details pulled successfully." This process is pretty straightforward in vanilla Selenium so i am not sure I am missing something else here.

I did try to switch to the alert before getting the text, but got the following error:

I used this : browser.switchTo().alert()

  TypeError: browser.switchTo is not a function
   - writing an ES6 async test case? - keep in mind that commands return a Promise; 
   - writing unit tests? - make sure to specify "unit_tests_mode=true" in your config.

Finally, If I remove the accept alert, then the test would give the following error since it is attempting to take a screenshot when it ends but gets blocked by the alert I am trying to validate. This gives me the idea that it is able to see the text somehow :

 Error while running .getScreenshot() protocol action: unexpected alert open: {Alert text : Bank Account Transaction Details pulled successfully.}

   Error: unexpected alert open: {Alert text : Bank Account Transaction Details pulled successfully.}

Code in question : (notice there are commented out parts due to testing, accepting the alert in beginning works, hence commented out)

I have added some commentary for reference in the code.

  //browser.acceptAlert() >> works
    let transactionsText= browser.switchTo().alert().getText(); >> gives us switchto not function error
    //browser.getAlertText() >> supposed to work but the console.log below does not print the required text
    console.log(transactionsText)

    if (transactionsText === "Bank Account Transaction Details pulled successfully.")
    {
        browser.acceptAlert()
        console.log('checking if the alert is accepted')
    }

Thanks in advance for your help.


Solution

  • Hope you found the solution, just wanted to post what I found here. I was looking for the same answer and, while I don't understand why this happens (if anyone could explain that would be great), I found how to get the text and store it in a variable.

    I found how to do it here

    And my code was

    browser.getAlertText((results) => {
            var alertText = results.value
            console.log(alertText)//just a quick test to see what it grabbed
        })