How to mark a test as xfail (expected failure)? - testing

I have a Testcafe test that looks for a link and clicks on it. Depending on the environment that I run it in, it may or may not be a valid link, and that is okay. What matters is that the link is found and clicked on, not that it goes to a valid page. For example, it may go to a domain that does not exist.
How can I ignore the error resulting from the click() call? I tried suppressing it in a try/catch, but it did not work.
Another alternative, how can I mark the testcase as XFAIL, so that it will be treated as an expected failure?

Unfortunately, TestCafe does not have built-in tools to suppress this error or treat a test as an expected failure. The only way to suppress the error on clicking on an invalid link is to use try/catch.
However, if your only goal is to find the link on a page, you can use Selector.exists to verify that the selected element exists.

Before clicking on a link you can check whether link exists and it is visible like
await selector.exists && await selector.with({ timeout: 5000}).visible

Related

testcafe Expect click to fail

Is it possible to expect a click to fail with testcafe?
I'm using testcafe studio.
After I hit a submit button on my page, I want a test to make sure that some text is no longer on the page.
What is the best way of doing that?
Thanks
I found one way of ensuring that the element wasn't on the page:
I used a t.notOk(Selector(mySelectorHere).count)
In your test scenario you can use exists Selector property:
Selector('h1').withText('Some text').exists

Auto login to website using script or bookmark

I've been trying to figure this out using various different methods. I'm trying to create a script/bookmark or some type of quick action to open a browser tab or window with a specific URL, and automatically log me in using my credentials. I'm not all that concerned about security for this at the moment.
At first I figured I'd try to use a javascript bookmark to do this, but nothing I found in my research worked. Next I tried to create a bash script, but I couldn't figure out how to send the credentials in via the terminal. Most recently, I literally copied the source code of a site, created a local file and tried to hack together something where I could prefill the form data with credentials and use JS to submit the form, and I've gotten close with this, but for some reason when I use the JS submit function, it errors out and says that the username and password are invalid. But when i turn off the submit function and manually click "log in" on my local html page, it works as expected. I want this to be a one click process, so the idea of using onload/submit or something to that affect is really important to me.
The site I'm testing with has a Rails backend and my next attempt might be trying to use POST to do what I'm thinking, but that's currently outside of my level of knowledge on the subject.
Anyone answering: i do not want to use a password manager to accomplish this.
My requirement is that i will either be able to a) run a script or b) use a 1-click option to do this per website. Ideally i'd be able to set this up in a sort of programmatic way to do this with multiple sites, but I'd be happy with 1 at the moment.
i know similar questions have been answered before, but I haven't been able to use information from those posts (the ones I've seen anyway) to figure out a good way to do this.
Create a bookmark for the current page you have opened.
Edit the bookmark
Change the value for the URL to something like this.
(javascript:(function(){CODE_GOES_HERE_FROM_BELLOW})();
find the field for username and password on the page.
Given example for hotmail
var inputs = document.getElementsByTagName('input'); for(var i=0;i<inputs.length;i++){if(inputs[i].name === 'passwd'){inputs[i].value = 'YOUR_PASSWORD'}else if(inputs[i].name === 'loginfmt'){inputs[i].value = 'YOUR_USERNAME'}}; document.getElementById(document.getElementsByTagName('form')[0].id).submit();
OR
try out casperjs.
The proposed solution didn't work for me and rather than spending tons of time installing a testing framework that I'll never use other than for this purpose, I decided to try to do this another way.
First, I found out that the reason my JS wasn't working before is because the site did not allow a JS submit to be done, or atleast that's what it seemed to be when I got this error: "Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience"
The javascript I was using was in fact working, just not submitting. I used the following code to fill the fields (using "Class Name" elements on the page since there was no name or ID):
document.getElementsByClassName('username')[0].setAttribute('value', 'user');
document.getElementsByClassName('password')[0].setAttribute('value', 'password');
As I mentioned, the problem was when I tried to use JQuery to submit the form: document.getElementsByClassName('loginForm')[0].submit();
Which is when the above error cropped up. I can't really say for sure whether this is the root of the cause, but the page does submit, but I get an invalid username/password error when I do
I haven't figured out a great way to get around this just yet, but my short-term, "hacky" solution was to use Applescript to send a return keystroke to the browser to submit the form. I'd ideally like to figure out how to get the submission to work using JQuery, but I'm not sure how to get around it.

How do I check for error message popup in browser using webdriver?

I have login form on website,
i want to make automatic testcase for this scenario:
i give phone number but no password:
How do I test selenium to give me expected red error message popup?
What function is in selenium api to check for it? I did not find it!
EDIT: or maybe my expected thing to happen should be "stay on same page"?
There is no specific function like that. Find out what is the xpath for that red colored text. Then you can create a web element using that xpath and check whether it is visible and what is the text value of that element, and assert on that.
As an example:
You first define the error message WebElement as messageElement
You may locate the element like this:
driver.findElement(By.id("error-message"))
Then you may get the error message text using this:
messageElement.getText());
Once you get the text you can then validate it with the expected text.
Assert.assertEquals(actualMessage, expectedMessage);
PS:
From snap, it doesn't seem to be another pop-up so no need to use switch to alert etc.

What is the proper way to test mandatory field in selenium

I am testing my web application using Selenium. All the form validation in the web application is done by HTML5 and some JS(for safari browser). I want to know what is the proper way to test the form validation.
Currently I am using one approach, i.e Before I filled up a mandatory field I clicked on the submit button. If the page is not refreshed then I assume the form validation working correctly. But I think there should be a better approach to do it. I am unable to find any proper solution. Any suggestion will be highly appreciated.
I also go through this link. But it is not working for me because it is not enough to have required attribute (eg required attribute does not work in older Safari browser).
Rather than checking if the page is refreshed or not, you should instead expect that it is not and that a certain error message or field highlighting or something is applied to the current page. When in an error state, the input fields probably get given an extra class, or an error div/span might be added to the DOM, try checking for that sort of thing

Selenium IDE - Assert that JavaScript redirect worked after clicking Ajax button

I have a button that executes an Ajax request and then it successfully redirects to another page.
How do I assert that the redirected page was successfully reached?
I have a clickAndWait on the button. But after that..?
you can use verifyTextPresent command to verify a unique lable or text in the redirected page.by that way you can fix you have successfully reached the redirected page.
try like this
command : verifyTextPresent
Target : some unique text in the redirected page.
i think your problem can be fixed by this.
IDE has many assert commands. You can use any of the one and you can achieve the test scope(here the page is navigated or not).
Example:
command : assertTitle
Target : check the title of the page.
In the above he used verifyTextPresent it will check weather the text is present or not in the page and it will continue the next step. If you use assert commands it will proceed the next step when the assert step is passed. Otherwise it fails.
One thing you keep in mind, selenium won't wait for Ajax kind of loading, it will wait for the Page loading. So, you have to put Wait commands explicitly to finish the Ajax loading.
You can get moreassertions when you convert the selenese code into the preferred language and testing framework. You can see that option in the Option tab in the IDE.
For more info on AssertCommands in SIDE