Gheeno San Pascual III
Gheeno San Pascual III

Reputation: 136

Nightwatch JS - IF Else IF assertion

Im new to Javascript( Nightwatch JS ) and programming in general.

I have an issue where with my If , Else If, Else test case, that does not proceed to the Else If statement.

I think it's because the condition in my IF Statement is an assertion ( .expect.element(); )

I was wondering if anyone can give me some pointers on what to do with my IF statement.

Essentially I want to verify :

If (checkbox is un-clickable) {
run set code; 
} Else if (this checkbox is clickable) {  
run set code;
} Else { 
.end();
}

Here's my sample test I did.

    ' Uninstall Missions Module ' : function(uninstallModule) {
    	uninstallModule
    	  .url('http://drupal8.develop:8091/admin/modules/uninstall')
    	  .setValue('#edit-text', "Missions")
    	  .pause(800);
    	  uninstallModule.expect.element('#edit-uninstall-missions').to.be.present;
    	  uninstallModule.waitForElementVisible('//*[@id="system-modules-uninstall"]/table/tbody/tr[31]/td[3]/div/div/ul/li/a', 1000, false);


    	if (uninstallModule.expect.element('#edit-uninstall-missions').to.not.be.enabled) {
    		uninstallModule.expect.element('body').text.to.contain('Remove mission entities')
    		uninstallModule
    		  .url('http://drupal8.develop:8091/admin/modules/uninstall/entity/mission')
              .verify.urlEquals('http://drupal8.develop:8091/admin/modules/uninstall/entity/mission');
            uninstallModule.expect.element('body').text.to.contain('Are you sure you want to delete all mission entities?');
            uninstallModule.click('xpath', '//*[@id="edit-submit"]');
            uninstallModule
              .pause(10000)
              .verify.visible("body > div.layout-container > main > div.region.region-highlighted > div > h2", "All mission entities have been deleted.");

    	} else if (uninstallModule.expect.element('#edit-uninstall-missions').to.be.enabled) {
    		uninstallModule
    		  .url('http://drupal8.develop:8091/admin/modules/uninstall')
    	      .setValue('#edit-text', "Missions");
    	    uninstallModule.click('#edit-uninstall-missions');
    	    uninstallModule.pause(800);
    	    uninstallModule.verify.urlEquals("http://drupal8.develop:8091/admin/modules/uninstall/confirm");
    	    uninstallModule.click('xpath', '//*[@id="edit-submit"]');
    	    uninstallModule.pause(1000);
    	    uninstallModule.verify.visible('body > div.layout-container > main > div.region.region-highlighted > div.messages.messages--status', "The selected modules have been uninstalled.");

    	} else {
    		uninstallModule.end();

    	}

    },		
}

Upvotes: 0

Views: 5755

Answers (2)

ghusse
ghusse

Reputation: 3210

There are two things to know about nightwatch (and other e2e testing frameworks)

  1. All actions are asynchronous. Even if the code can look synchronous thanks to the frameworks, in the end, everything is asynchronous. Reading values requires asynchronous code as well.
  2. When using expectations, if the code throws an error if the expectation is not verified; you cannot use them to read values.

With nightwatch, you can use getAttribute to read the value you need:

client.getAttribute('#edit-uninstall-missions', 'enabled', result => {
  // I'm not sure about the type returned in result.value
  // you can test it and adjust
  const enabled = result && result.value !== "false" 
                    && result.value !== false;

  if (enabled){
    // Continue your test here
  } else {
    // Another test here
  }
});

Upvotes: 4

Gheeno San Pascual III
Gheeno San Pascual III

Reputation: 136

Here's what I did.

    uninstallModule.getAttribute('#edit-uninstall-missions', 'disabled', function(results) {

          console.log(results.value);
          if (results.value == "true") {
            console.log("It's true bro");
          } else if (results.value != "true") {
            console.log ("It's null");
          } else {
            console.log("It's Neither");
          }
    });

Upvotes: 0

Related Questions