Clicking a button based on the text - automated-tests

I am using TestCafe to test a locally run application without problems besides the below :
I have an element that looks as follows:
Upload file
When the test attempts to click the element by using
.click(Selector('.btn').withText('Upload file'))
the following error is given
1) The specified selector does not match any element in the DOM tree.
  | Selector('.btn')
 > | .withText('Upload new file')
Any hint would be appreciated as to what could be wrong.

I've checked a button click on the Bootstrap site with a similar button and it works as expected. Please see the following code:
 
import { Selector } from 'testcafe';
fixture `bootstrap`
.page `https://getbootstrap.com/docs/4.2/components/buttons/`;
test(`click button`, async t => {
await t.click(Selector('.btn').withText('Success'));
});
 
I recommend you check your site and ensure that:
The button's offsetWidth and offsetHeight are greater than zero.
The button does not have styles like display:none or visibility: hidden.
The button contains exactly the same text as you are searching for.
 
If this does not help, It would be great if you share your project or website url to demonstrate the issue.
 
 

When TestCafe gives this error message: The specified selector does not match any element in the DOM tree, and I have no clue of what is happening, I take the following path:
I create a Selector variable that targets the element and hover over it before clicking on it:
const uploadFileButton = Selector('a.btn').withExactText('Upload file');
await t
.hover(uploadFileButton)
.click(uploadFileButton);
Notice that in the above code, I have tried to be as specific as possible when defining the Selector.
If, at runtime, you do not see the TestCafe cursor moving towards the target element (this means you don't run in headless mode), then you know that the Selector is wrong. It could be because the element has not mounted in the DOM at the time TestCafe tries to hover over it. To validate this point, modify the TestCafe code to:
const uploadFileButton = Selector('a.btn').withExactText('Upload file');
await t
.expect(uploadFileButton.exists)
.ok({timeout: 10000})
.hover(uploadFileButton)
.click(uploadFileButton);
If, at runtime, TestCafe stops on the .ok() line, then you know that the Selector is definitely wrong.
In this case, go to the developer tools and type the following command in the console:
var el = document.querySelectorAll('a.btn');
el <ENTER>
inspect the content of the el element. If you find the targeted button, then you should check the innerText property and you should check if there is no CSS rule that makes the text upper case or makes it invisible.

Related

How can I verify that a clicked element is a GoJS picture part and not an overlaid element?

I'm trying to implement a mask over my GoJS diagram when a details sidepanel is shown. This requires stopping propagation of click events to diagram parts beneath. How can I verify that the event target is the picture part and not the mask element which overlays it?
I've tried using the pointer-events CSS property, but that doesn't do the job.
Simplified markup:
<div id="diagramWrapper"></div>
<div id="sidebar-mask"></div>
Script snippet:
buildGo(go.Picture, {
source: 'images/icons/simplify-icon.png',
...,
click: function (e, node) {
if (e.diagram === node.part) { <-- this, right here, isn't right
let nodesToRemove = getLinkedNodes(e, node.part.data.key);
removeNodes(nodesToRemove);
}
}
})
I haven't gotten my head around the two-letter object properties that GoJS uses, which prevents me from easily finding the right property. Any insight into that would help greatly.
I suggest that you just set Diagram.isEnabled to false when you show a modal dialog. Be sure to set it back to true when the modal UI goes away for any reason. More possibilites are listed at https://gojs.net/latest/intro/permissions.html.
As documented at https://gojs.net/latest/intro/index.html#DevelopingYourDiagram, you should only be using documented API names in your code -- not the minified two character names which will change with each build.

TestCafe - How can I determine the mouse cursor (pointer) shape when over certain HTML elements

I am trying to write a TestCafe functional test around drag and drop. I already have drag and drop working and I am trying to test the source/target functionality by trying to drag an element onto a part of the screen it is not allowed to drop. The mouse cursor shape changes to the "No entry" symbol when the drop is not permitted during the mouse hover. I cannot see anywhere in the TestCafe documentation where I can interrogate the mouse cursor icon shape.
I cannot see any documentation on this in TestCafe.
Does TestCafe support this?
Thanks
Mark
TestCafe selectors specify an element on the testing page and return an object with a set of properties and methods to the server.
Read more about Selector's properties in TestCafe docs.
You can use the getStyleProperty method to verify the cursor style of an element, e. g.
.expect(Selector('div').getStyleProperty('cursor')).eql('no-drop');
In any other cases, when you need to get specific information about a DOM element, you can use a combination of ClientFunction and Selector. This scenario may look as follows:
const el = Selector('#editable-div');
const getCursorStyle = ClientFunction(() => {
return window.getComputedStyle(el()).cursor;
}, {dependencies: {el}})
await t
.drag(Selector('#create'), -14, -255, {
offsetX: 38,
offsetY: 11
})
.expect(getCursorStyle()).eql("no-drop");
You can even use a more advanced approach - implementing addCustomDOMProperties, especially if you need to check the added property several times.

How to test pointer-events: none with Protractor and Jasmine?

Under certain circumstances I am setting the css property "pointer-events:none" to avoid an element being clickable (it is a complex element which cannot be disabled one by one and which it is wrapped by a div and this div has the pointer-events to none)
So what used to be clickable and tested like that, it is not anymore.
When I try to test it I always get this:
Failed: unknown error: Element is not clickable at point (167, 407). Other element would receive the click:...
Which is expected because as the element is not clickable, the parent element gets the click.
My question is, how to test with Protractor/Jasmine an element which is not clickable by setting this property (pointer-events)?
By the way, the commonly used methods isEnabled and isDisplayed, both of them return true.
I also thought about creating a test which expected the error (although I would prefer a cleaner solution), with toThrow() or ToThrowError() but it didn't work at all.
Thanks
You can try special EC for this -
http://www.protractortest.org/#/api?view=ExpectedConditions.prototype.elementToBeClickable
Example:
var isClickable = protractor.ExpectedConditions.elementToBeClickable($('your element here'));
expect(isClickable()).toBeFalsy('Element expected not to be clickable');
I met the same problem - how i do assertion that i cannot click on element -
myElement.click().then(
() => {
fail("Element should not be clickable for Observer");
},
(err) => {
expect(err.message.toString()).toMatch("Element is not clickable at point");
});
Putting error callback and asserting that correct error was thrown.

Is there a way to forcefully find a CSS selector element/bring it into view?

Let's say for example I'm going through my stylesheet but I can't remember what element a certain CSS selector affects.
Is there any method, tool, or anything that I could do/use to find out what exactly it is affecting?
Thanks!
I just opened up a random bootstrap template site and did what you where asking for.
Open up your chrome browser (I prefer this as I feel this is easy to debug both Jquery and css) and Press F12, you will get a developer window as in the image.
Switch to the console tab.
Use Jquery selector to select all
the intended elements (you can use the same css selector here too
but just place them inside $('')) Eg: $('.tab-content') I am trying to find out all the elements with the class tab-content
The result is all the elements
of that selector.
NOTE: This appraoch woud require you to have Jquery loaded into your page. Else the script will throw an error saying $ is not defind.
In addition to using your browser's development tools, there are two easy ways to do it that should work in almost any browser (no matter how bad the developer environment).
Visually
Temporarily set a border or background color for the selector, like:
border: 1px solid red;
Or:
background: red;
This makes it very easy to find the affected elements.
Programmatically
On the JavaScript console, use:
// Replace with something that prints the relevant details
var stringify = function(element) { return element.innerHTML; };
// Iterate over all affected elements and print relevant info
var affectedElements = document.querySelectorAll('.your .selector');
var len = affectedElements.length;
console.log('Elements affected: ' + len);
for (var i = 0; i < len; i++) {
var affectedElement = affectedElements[i];
console.log(
'Element ' + (i+1) + ':\n' +
stringify(affectedElement) + '\n\n');
}
The inspection of elements feature of the browser is meant for the purpose you want.
1.Open up the index file in any browser(preferably Mozilla Developer edition),
2.Right click and Inspect element,
3.Then open the compiled stylesheet. Find out the style element you want to check the effect of.
4. Go back to inspection, remove/add CSS properties and see the effect in real time.
The other way is to use Adobe brackets. It's live preview feature will highlight the section that involves the code snippet, you point your cursor to.

How can i test Style property's values of an element by nightwatch.js

I am testing a web app by nightwatch.js, i have an element which has the style property, when i click on this element its style property changes, i want to test this scenario by nightwatch.js.
By nightwatch i can click on element but unable to test style property's values. If there is any way to test style property's value of an element by nightwatch then please let me know.
my html element is like following.
<div id="wrapped_class" style="display: none;"/>
when i click on this element like following
browser.click('#wrapped_class', function (client) {
client.waitForElementVisible('#wrapped_class[style=display block;', 5000);
});
but above is not working for me. :(
Please help!
You can use Nightwatch's expect to check if a given css property of an element exists and if it has the expected value:
browser
.click('#wrapped_class')
.expect.element('#wrapped_class').to.have.css('display').which.equals('block')
You can read more about it in the Nightwatch API documentation

Resources