is there a way to call waitForElementPresent in nightwatch without erroring out if the element is not there?
Right now, if you pass in false as the third parameter, you still see an error. Ideally, I would like a function that waits for an element, and returns if it didn't find the element.
you can do it :
Browser.waitForElementPresent('#element',3000,false, function(result){
If(result.value === true){
// test if element present
}else{
// test if element not present
}
})
Since the day one, i did this and the problem with this code is nightwatch would count a failed test as a passed test,as you see above code handle both result value.
So i recommend let the nightwatch return error itself, write difference function for difference value.
Related
I'm writing a test in cypress to test if a video is playing. Unfortunately, this can't be achieved through code such as:
cy.get('video')
.should('have.prop', 'paused', false)
.and('have.prop', 'ended', false);
I do have an a solution in mind that I'm having trouble executing. The video element has a property currentTime that will be higher than 0 if the video is streaming. I've tried several ways of declaring this to a variable and making an assertion based on that variable.
cy.get('video')
.should('have.prop', 'currentTime')
.then((x) => {
// x is the class prop
expect(x).to.be.greaterThan('0');
});
However, cypress doesn't interpret the value as an integer. Any advice on how I could go about this problem?
The currentTime prop is already a number, but you compare it a string, so Cypress gives you the message
AssertionError: the argument to above must be a number
This refers to '0', not x.
Try
expect(x).to.be.greaterThan(0);
I am trying to debug a testcafe test with node from VSCode and want to verify that the selector used in the code identifies the correct element and retrieve the values of the variables that is declared in a function / variable assigned with a selector.
I start the test.js file in debug mode with command:
"C:\Program Files\nodejs\node.exe" --inspect-brk=21496 testcafe.js chrome tests.js --env=dev --skip-js-errors
The test stop at the breakpoint and when the below line is reached, i wanted to verify what exactly is inside that variable (element) such that i can verify if the selector is selecting the desired element.
let element= Selector(".unique_selector_class").parent(2);
I expect to find the properties of the selected element in the debug mode. e.x., length of the 'element' if its an array, outertext of the element.
Update:
I think what i said earlier was little confusing. I have a method like this which is called by a test.
`async deleteSelectedComponentsMethod()
{
let element = await Selector(".uniqueSelectorClass");
let numberOfSelectedComponents = element.length;
for (let i = 0; i < numberOfSelectedComponents; i++)
{
await t.click(deleteSelectedComponent.nth(i));
}
}`
In this method i wanted to see what is inside the variable 'element', so that i can write a logic as in the code. PS: the element am trying to identify will be visible only when the mouse is hovered.
The value in the variable 'element' returns a function that does not help to find the runtime values in the element
UPDATE:
Selector doesn't return an array when multiple elements match it. Use await selector.count to retrieve the number of matched elements and selector.nth() to enumerate them:
async deleteSelectedComponentsMethod()
{
let element = Selector(".uniqueSelectorClass");
let numberOfSelectedComponents = await selector.count;
for (let i = 0; i < numberOfSelectedComponents; i++)
{
await t.click(deleteSelectedComponent.nth(i));
}
}
Read more about the selector.count property in the Using Selectors article.
ORIGINAL ANSWER:
You can use the await keyword to retrieve information about elements represented by Selectors. Once we will implement this feature: #3244, you will be able to debug Selectors by typing selector expressions in the browser console.
<g class="ABC" transform="translate(786.9,53)" score="1.3">
How can I query the score value of this example HTML tag?
I am writing a test to query the score attribute (it is 1.3 in the above example) and compare it with the correct number in my test.
I am not sure how to get the score attributes value. cy.get() didn't give me what I need.
Thanks.
Cypress gives you direct access to the DOM. If you want to target a DOM object and operate on it directly, you will want to use the then() function.
Here's an example that should work in your case:
cy.get("g.ABC").then(elem => {
let score = elem.attr("score");
cy.log(score);
// etc...
});
See this doc page for more info.
If you want to perform a mocha assertion directly on the value, use should() to take advantage of automatic retries:
cy.get("g.ABC").should(elem => {
let score = elem.attr("score");
// If this assertion fails, the entire block will retry until it doesn't.
expect(score).to.equal("1.3");
});
See this doc page for more info.
I want to wait for an element to be visible, and if it is do X without erroring it out. I trying using the nightwatch.js waitForElementVisible but that errors out if the element is not visible. What should I do?
waitForElementVisible takes a param abortOnFailure to control if command should fail or not
http://nightwatchjs.org/api#waitForElementVisible
Possible workaround is to use the function below, but the drawback is that we are setting static timeout,
client.pause(1500).element('css selector', '.css_selector', result => {
if (result.status > -1) {
browser.expect.element('.element_to_check').text.to.contain('YES')
}
})
This way nigtwatch will check your element and won't throw an error if it's not in DOM.
I'd like to use CasperJS to evaluate a variable equals a certain value.
I simplified my exemple as much as I could that way:
var testDate = "24/03/14";
casper.test.begin('TEST', 1, function suite(test) {
casper.start('http://www.google.com/', function() {
this.test.assertEval(function() {
return testDate == "24/03/14";
}, "testDate is 24/03/14" );
});
casper.run(function() {
this.test.done();
});
});
I don't know why it fails, here is what I get in my console:
Test file: tests.js
#TEST
FAIL testDate is 24/03/14
# type: assertEval
# file: tests.js:7
# code: }, "testDate is 24/03/14" );
# subject: null
# fn: undefined
# params: undefined
FAIL 1 test executed in 2.896s, 0 passed, 1 failed, 0 dubious, 0 skipped.
Details for the 1 failed test:
In tests.js:7
TEST
assertEval: testDate is 24/03/14
Any idea ?
UPDATE
I realised my simplified example was faulty, it didn't represent what I really needed.
Actually, what I want to achieve is to test if a variable from the current page DOM context equals a local variable.
As per manual Asserteval:
Asserts that a code evaluation in remote DOM strictly resolves to a boolean true:
your testdate variable is local to the casperjs script and is not accessible in the remote dom. You would have to inject it to the window like described here.
Ok found the answer myself.
To test if a variable from the current page DOM context equals a local variable, I realised I could use a simple assertEvalEquals():
test.assertEvalEquals(function() {
return variableFromPageDomContext;
}, localVariable);
Likewise, when testing if a variable from the current page DOM context matches a RegExp pattern, we have to use evaluate() to get the variable from the DOM as the first parameter of an assertMatch():
test.assertMatch(this.evaluate(function() {
return variableFromPageDomContext;
}), RegExpPattern);
Hope that can help.
As #Surreal answers its possible to use the assertEvalEquals() passing the function and the expected value.
However the original question wants to pass a variable from casperjs context to assertEval() function, you can simply do it as follows, passing to assertEval() three arguments: the function which receive the value, a message for the assert and the value:
var varPassToEval = 'someValue';
test.assertEval(
function(varFromCasperContext){
return varFromPageDomContext === varFromCasperContext;
},
'Assert Eval to test',
varPassToEval
);
With the above example probably is clear to use assertEvalEquals() however could be useful for more complex cases, for example imagine that you want to check if a text appears in a some <li> inside <ul> in DOM which it's dynamic and can change but you don't know at first where your text is... for this case you can use:
var somePartOfText = 'blue';
test.assertEval(
function(varFromCasperContext){
return document.getElementsByTagName("ul")[0].textContent.indexOf(varFromCasperContext) != -1;
},
'Assert Eval to test',
somePartOfText
);
Hope it helps,