How to perform soft assert in protractor cucumber - protractor

defineSupportCode(({Given, When,Then})=>{
When('click on search button', async () =>{
await browser.actions().sendKeys(protractor.Key.ENTER).perform();
await browser.sleep(3000)
await browser.actions().sendKeys(protractor.Key.ENTER).perform();
await browser.sleep(3000)
let element1 = element(by.xpath("//h3[text()='Selenium - Web Browser Automation']"))
await expect(element1.isDisplayed()).to.eventually.be.false;
});
Then('it should show search result', async () =>{
let element2 = element(by.xpath("//h3[text()='Downloads - Selenium']"))
await expect(element2.isDisplayed()).to.eventually.be.true;
});
})
My last then block was skipped.But I want to continue my all test steps even if it is pass or fail (then definitely i think i have to use soft assert).In testng we have a class called softassert. Is there something available here as well similar to that one. I am using chai package for assertion purpose
TIA

Background:
This is not a protractor or cucumber issue, it is actually chai(well it looks like you are using chai from your code snippet but feel free to correct me if I'm wrong).
Issue:
After reviewing chai docs https://www.chaijs.com/api/assert/ it looks like they do not support the concept of soft asserts like some other assertion frameworks.
Solution 1:
The only workaround I can think of is to wrap your assertion in a try catch and store the error received from the assertion in your world instance, then in your after hook throw the errors.
Solution 2:
Use a library that does support soft asserts. From a quick search, I see https://www.npmjs.com/package/soft-assert but FYI I have not tried this library
Notes:
One bad thing about this is that screenshots may capture incorrect information.

Related

Undefined name 'Utils' error in catch exception

I am working on Flutter Firebase send verification email page, but I am getting the Undefined name Utils error after adding a catch error exception. Below is the snippet of the code:
Future sendVerificationEmail() async {
try {
final user = FirebaseAuth.instance.currentUser!;
await user.sendEmailVerification();
setState(() => canResendEmail =` false);
await Future.delayed(const Duration(seconds: 5));
setState(() => canResendEmail = true);
}
catch (e) {
Utils.showSnackBar(e.toString());
}
}
I could be wrong, but I think whoever wrote that code also wrote a Utils class with that function as well. It's not part of any standard library. And the exact implementation we can't know. Obviously it's some helper method to show a Snackbar. It usually requires a BuildContext, but you could check out How to show snackBar without Scaffold to see alternatives.
And a quick Google search makes me think that you simple copied that code from How to use google login in flutter and bypass emailverification since it has the exact same method. I apologize if that's not the case. My advice is to not simply copy pasting code without understanding what's happening. Especially code that is not part of a guide.
In any case, the code that doesn't work shows a message to the user through a Snackbar in case an exception happens. Which isn't really a user-friendly message, so it might be not that great to write it like that anyway.
I think you haven't added
import 'package:basic_utils/basic_utils.dart';
Or, make sure you added this in your pubspec.yaml file:
dependencies:
basic_utils: ^4.2.2

Protractor - get result of sendChromiumCommand

The goal: obtain a result of chromium command execution, e.g. Profiler report data
What I've done: I'm able to call chrome devtools command using browser.driver.sendChromiumCommand.
E.g. await browser.driver.sendChromiumCommand('Page.reload', {}) reloads page, so I can confirm that this is working.
The problem: the result of any command execution is always null
Here is my code
describe('Protractor Demo App', function() {
it('whatever', async function() {
await browser.get('http://juliemr.github.io/protractor-demo/');
const data = await browser.driver.sendChromiumCommand('Page.reload', {
scriptToEvaluateOnLoad: `(function(){return '123 })()`
});
console.log(data); // expected '123' but got 'null'
});
});
Note: the issue isn't connected to a particular command, all of the devtools commands return null
This is not a Protractor issue per se.
The problem is that underlying selenium-webdriver for Node.js uses send_command(which does not return result), but does not implement usage of send_command_and_get_result.
According to response from developers it's going to be fixed in next alpha release (current one is 4.0.0-alpha.7)
I suppose that, in order for this to work, Protractor's sendChromiumCommand is gotta get a sendChromiumCommandAndGetResult counterpart, once selenium-webdriver fix is available.

what is the best approach to handle promises with chai-as-promised?

I just want to know that whether this approach is good or not while using chai-as-promised with mocha and protractor?
let options = customers.all(by.tagName('option'));
options.then(function (items){
for(let i=0 ; i<items.length ; i++){
items[i].getText().then(function(txt:any){
if(txt == "ABC XYZ"){
items[i].click();
}
})
}
});
First of all you should learn more about chai plugin chai-as-promise. Visit This url to know more about this plugin.
Second you need to understand that this plugin is meant for handling assertion statement that has dependency on some promise. It is not meant for dealing all the promises. If you are new then you should learn using async/await with protractor.
Refer this post for more details
first source
second source

Protractor element.click() throwing an exception

I was trying to figure out why .click() below was crashing protractor :
this.clickSecondPanel = function () {
element(by.css('div.panels-gs.panel-top-two-gs')).click();
}
until I changed the line to :
element(by.css('div.panels-gs.panel-top-two-gs')).click;
where my spec.js looks something like :
var DataCardPage = require('./pageObjects/dataCard.page.js');
var dataCardPage = new DataCardPage();
describe('Clicking on the 2nd panel', function () {
dataCardPage.clickSecondPanel();
it('Should select the 2nd test panel', function () {
expect(dataCardPage.getSecondPanelText()).toBe('TEST123');
});
In other places in my code, I use .click() (with parenths), so this is confusing to me.
The error is nasty:
Started
[17:44:23] E/launcher - Error while waiting for Protractor to sync with the page
: "window.angular is undefined. This could be either because this is a non-angu
lar page or because your test involves client-side navigation, which can interfe
re with Protractor's bootstrapping. See http://git.io/v4gXM for details"
Any advice appreciated...
Bob
Solved this in the comments above, posting as an answer.
My suggestion was to try moving the clickSecondPanel() inside the it block. It looked suspicious by itself just from a "best practice" perspective as I do not have any code that is outside of a jasmine function i.e. it, beforeAll, afterAll etc (don't even know where I learned that habit honestly).
It also seemed to effect the control flow and asynchronous execution so the click() event was triggering too soon. This can be explained in part by this documentation and/or this blog post
Try using browser.ignoreSynchronization=true at the begining of your test. May be the application that you are trying to automated does not contain angular in it.

Protractor - getText as input to sendKeys and other actions

I am writing a protractor test, where I need to read a span/div with id='mylabel' using getText(). I then need to pass the value to an input (id='myinput') using sendKeys().
So, I do this:
var value;
element(by.id('mylabel')).getText().then(function(txt){
value = txt;
element(by.id('myinput')).sendKeys(value);
// do "other protractor tasks" with 'value'.
})
But, is there a way I can avoid the nesting, by asking protractor to perform sendKeys and subsequent actions only after the value variable is set?
The above is a simple case, but I soon find code getting into multiple nesting because of the waiting for promises to be resolved. Also, I observed that protractor does not provide a stacktrace if "other protractor tasks" throws an error due to an error somewhere down the line (it just hangs and times out).
I am using Protractor 2.1.0 and I am working with Angular JS pages.
I am specifically interested to know if it is a known issue to have silent errors in nested tasks with Protractor and is there anyway to solve it?
Protractor handle at least one level of promises without the need of then function. That way you can expect synchronous flow.
If you are looking for event based action like watching a value to update then you can setup something like this:
function waitForTextToUpdate(elm, defaultText, timeout) {
if (typeof(timeout) === 'undefined') {
timeout = 10000;
}
return browser.driver.wait(function() {
return elm.getText().then(function(value) {
return !(value.indexOf(defaultText) > -1);
});
}, timeout, "Expectation error (waitForTextToUpdate): Timed out waiting for element state to change.");
}
Promises are inevitable in protractor. There is no way to avoid handling promises, but if you want to avoid nesting it can be done easily using .then() chaining functionality. Here's an example -
var value = '';
element(by.id('mylabel')).getText()
.then(function(txt){
value = txt;
})
.then(function(){
element(by.id('myinput')).sendKeys(value);
// do "other protractor tasks" with 'value'.
});
There's also an npm package available for this feature. Q npm package. It works similar to the above example but is more extended.
Hope this helps.