PS: driver.switchTo().alert().accept(); is not working
Well , I want to automate testing of webpage which shows random alerts*
*random alerts :JavaScript Alert which are not controlled and can appear any time
so I dont know where to put driver.switchTo().alert().accept()because the occurrence of alert is not known in advance.
So kindly help me.. Here is simple mocha selenium test... I have commented out the alert.accept() part..
I want to know how to handle
UnexpectedAlertOpenError: unexpected alert open: {Alert text :}
once in for all....
I read the selenium docs but couldn't find any...
here is my code
describe( 'handling alerts' , function(done){
after(function(done){
return driver.quit();
done();
});
it( 'should check title ', function(done){
driver.get("https://www.google.in");
driver.findElement(By.id("q"));
driver.get("https://jsfiddle.net/cwthh0y7/");
// driver.switchTo().alert().accept();
driver.getTitle().then(function(title){
assert.equal(title, "Edit fiddle - JSFiddle");
done();
});
});
});
You can set capabilities while defining your driver object.
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/ie/InternetExplorerDriver.html#UNEXPECTED_ALERT_BEHAVIOR
Refer this link on how to set Capabilities. This will help you define a generic behavior when your alerts are intermittent.
Related
Hellow there! I am using the wdio/cli so I created the wdio.conf.js with this command, then I start doing the test. But the issues is when have more than one test in a single or multiple test files.
In the test file I have something like this:
beforeEach(async function() {
$('~home').waitForDisplayed(81000, false);
});
Where home tag is a tag in the first view when the app runs in first screen. And appear this error:
element ("~home") still not displayed after 10000ms
So need to do kind of driver.resetApp()/ But dont know how to do it, what import do I need to do etc.
Did you try resetApp? You can't user driver as "main object" - Everything is under browser variable. Try this
//async
await browser.resetApp();
//sync
browser.resetApp();
Check Appium doc + wdio documentation.
I am working on the development of an AngularJS app, and have recently been asked to look at integrating some automated testing into our development process. I have not used automated testing at all before, so am still getting to grips with it.
The framework that I have decided to use is Protractor, and I'm just looking at starting to write some basic tests, however, I'm having a bit of trouble getting hold of some of the HTML elements from within the spec.js file where my tests will be written. I have the following describe block in spec.js:
describe('My App', function() {
var loginForm = element(by.id('loginForm'));
var loginBtn = element(by.className('form-control'));
var usernameInputField = element(by.id('inputName'));
var passwordInputField = element(by.id('inputPass'));
it('should hit the VM hosted site', function() {
browser.get('http://192.168.x.xyz:8080');
expect(browser.getTitle()).toEqual('My app');
});
//Test to log in:
it('should enter the username into the username field on log in form', function() {
usernameInputField.sendKeys('username');
passwordInputField.sendKeys('password');
expect(loginForm.usernameInputField).toEqual("admin");
expect(loginForm.passwordInputField).toEqual("password");
});
});
My conf.js file currently looks like this:
exports.config = {
framework: 'jasmine',
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['spec.js'],
multiCapabilities: {
browserName: 'firefox'
}
}
When I run the command protractor conf.js from the command line, I get some output that says:
Failed: No element found using locator: By(css selector, "[id="loginForm.inputName"])
I have 'inspected' the input field for the username on the form in the browser, and it shows that the HTML element's 'name' attribute is 'inputName', and that that element is nested within the loginForm element.
I don't understand why the console is giving me the error that says it's trying to find the element using a 'CSS selector'- I'm expecting it to try and find the element by it's HTML tag's 'name' attribute... Clearly I'm doing something wrong in how I'm writing the 'test to log in', because if I comment that out, and run protractor conf.js again, I get 0 failures- so the other test is running successfully...
What am I doing wrong here? How can I get the form input fields and enter correct log in details into them to check that the 'log in' feature works correctly?
As an aside, although I will want to test the 'log in' capability too, is there a way to 'skip' this during testing, so that I can run the tests on all of the features of the app, without Protractor having to log in to the app every time to run the tests?
Edit
I've changed the variable definition of the form elements that I want to get, so that they're now:
var loginBtn = element(by.className('form-control.submit'));
var usernameInputField = element(by.id('loginForm.inputName'));
var passwordInputField = element(by.id('loginForm.inputPass'));
and have also changed the test to:
it('should enter the username into the username field on log in form', function() {
usernameInputField.sendKeys('username');
passwordInputField.sendKeys('password');
loginBtn.by.css('button.button.primary').click();
expect(loginForm.usernameInputField).toEqual("username");
expect(loginForm.passwordInputField).toEqual("password");
});
and this seems to have cleared up the No element found using locator... error message that I was getting. However, I'm now getting another error that says:
Failed: Cannot read property 'css' of undefined
on the line:
loginBtn.by.css('button.button.primary').click();
I don't understand why this is... Can anyone explain what I'm doing wrong here? How would I resolve this error?
This is where Your test fails:
loginBtn.by.css('button.button.primary').click();
You should change it into:
loginBtn.click()
Try replacing
var loginBtn = element(by.className('form-control.submit'));
by
var loginBtn = element.all(by.css('.form-control.submit')).get(0);
I'm using Protractor with Cucumber to write some tests but I'm stuck at some point. In step after login, I'm rerouting to another page using browser.get(url) function provided by protractor. But it always returns before the page is completely loaded. I have tried many solutions so far but no luck. I have tried with browser.wait, browser.get(url).then(function(){ // code when it loads}) but Im getting 0 positive results.
Here's my code:
// Steps will be implemented here
this.Given(/^I am logged in as user \'([^\']*)\'$/, function (user, callback) {
console.log('USER: ' + user);
browser.driver.get('https://cit-was70-l06/ipa')
browser.driver.findElement(by.xpath('my_xpath')).sendKeys(user);
browser.driver.findElement(by.xpath('my_xpath')).sendKeys(user);
browser.driver.findElement(by.xpath('my_xpath')).click().then(callback);
});
this.Then(/^The screen is shown, with title \'([^\']*)\'$/, function (title, callback) {
console.log('Title from feature file: ' + title);
var url = 'some/url/in/application/';
browser.get(url).then(function(){
// This portion executes before page is completely loaded.
// So if I try to get any element, it throws me an error.
// [15:32:13] E/launcher - "process.on('uncaughtException'" error, see
// launcher
// [15:32:13] E/launcher - Process exited with error code 199
// It works if I add static delay to wait for page load completely
// but that will not be a good solution if I have too much pages to test
callback();
});
console.log('After page laoad');
});
Any suggested work around will be much appreciated.
[15:32:13] E/launcher - "process.on('uncaughtException'" error, see launcher
[15:32:13] E/launcher - Process exited with error code 199
The above error can be caused due to various reasons mostly related to promises. But it should throw the correct message. There is already a work around provided here https://github.com/angular/protractor/issues/3384 to catch the exact error message.
You could change the launcher.ts file in your protractor dependency as mentioned in above forum to catch the error inorder to debug your issue.
And I would suggest you to return your promises instead of callbacks when writing step definitions in protractor-cucumber, in this way cucumber would know when to complete its async actions.
Try the below code.check whether it helps.
browser.get(url);
browser.waitForAngular();
then try to call your function.
Use protractor.ExpectedConditions to check visibility of any elements on page which will be displayed after successful login. Write a customized method as shown below.
If element displayed, then navigate other page using browser.get();
Code Snippet
EC = protractor.ExpectedConditions;
//targetElement=element(locator);
this.isElementVisible = function (targetElement, timeOut) {
'use strict';
timeOut = timeOut !== undefined ? timeOut : 8000;
browser.wait(EC.visibilityOf(targetElement),
timeOut).thenCatch(function()
{
assert.fail(' element is not visible');
});
};
What are the methods we can use to wait for an Angular site to be loaded in order to test it with protractor in order to avoid this error caused by jasmine : A Jasmine spec timed out. Resetting the WebDriver Control Flow ?
I'm able to make the login and go to home page that test is passed, but from the second test i have problems of jasmine.
I have configured this problem by adding this function into my config file :
onPrepare: function() {
return browser.getProcessedConfig().then(function(config) {
var browserName = config.capabilities.browserName;
browser.manage().timeouts().setScriptTimeout(60000);
});
});
You can use the browser object of Protractor to wait for angular.
As soon as you load your page add the following :
browser.waitForAngular();
This error means that your test took too much time and exceeded the default Jasmine spec timeout interval which is 30 seconds by default (It looks like you've configured the timeout to be 60 seconds). It can be configured in the jasmineNodeOpts object in your Protractor config:
jasmineNodeOpts: {defaultTimeoutInterval: timeout_in_millis},
The solution is usually use-case specific and it usually indicates there is an error in the test code. In order to fully understand what is going, we would need to see the code itself.
In your particular case, for starters, you should try moving the "ignore synchronization" and the browser.get() part into the beforeEach. Also, since you are turning the sync off, you need to wait for the element to be present on the page before interacting with it:
describe("my app", function () {
beforeEach(function () {
browser.ignoreSynchronization = true;
browser.get("...");
});
it("should make the login test", function () {
// ...
var EC = protractor.ExpectedConditions;
var username = element(by.model("credentials.username"));
browser.wait(EC.presenceOf(username), 10000);
username.sendKeys("RET02");
// ...
});
});
And, I am not sure if you really need to turn the synchronization off since this is an AngularJS page you are working with.
Can you wait for a url?
Let's assume that when you click on the login button your page is redirected to another url. So you can wait for the expected url. Example:
browser.driver.wait(function() {
return browser.driver.getCurrentUrl().then(function(url) {
// Dashboard is the loaded url after login (in this example)
return /Dashboard/.test(url);
});
}, 60000);
This code waits for the page browser.baseUrl/Dashboard to be loaded, for 60 seconds
I am new to angular and protractor.We use protractor for functional testing and integrated with jenkins.
Problem
In some screens we use ng-toast to show toaster messages(mainly for server response messages like 'save filed' etc.).But protractor could not catch these, since it will wait for all angular timeouts(including toaster timeout) to complete.Showing error:
Timed out waiting for Protractor to synchronize with the page after 11 seconds.
I tried to set ignoreSynchronization too.
How to tackle this. I am really stuck on this..
After a long search on google I got answer.We can make use of promises with browser.waitin test cases those needs to wait for toaster messages.
.....
browser.wait(function() {
var deferred = protractor.promise.defer();
getToaster().then(function(){
deferred.fulfill(true);
expect(getToaster().isDisplayed()).toBe(true);//and other assertions
});
return deferred.promise;
});
.....
This is well described in this blog
Also more details about protractor.promise can be found here
Alternatively I did it in another way as:
...
browser.manage().timeouts().implicitlyWait(10000);//set timeout for element
expect(toaster.getToaster().isDisplayed()).toBe(true);
browser.manage().timeouts().implicitlyWait(1);//reset
....
But in a protractor way of doing is browser.wait with ExpectedCondition which is described in the protractor api including custom conditions.I am currently using this explicit wait approach.