CodeceptJS closing browser window after finish - testing

I´m new to CodeceptJS with Puppeteer. I want to let my window browser open after the finish.
Feature('Login');
Scenario('test', (I) => {
I.amOnPage('https://google.es')
});

Just put pause() as the last line of the Testcase
Feature('Login');
Scenario('test', (I) => {
I.amOnPage('https://google.es')
pause();
});

Related

How to close the safari pop up dialogue when running automate script with nightwatch on BrowserStack?

I use Browserstack to do the E2E testing, now I met a problem when I try to run the mobile automate script in safari on Browserstack, there will have a pop-up dialogue show when I click a button which will result in opening a new tab, the dialogue show message like this: 'this site is attempting to open a popup window', I must close it and the script can continue executing.
Now the problem is:
1. When I click the button which will trigger this pop-up dialogue, there will always show an exception in the log: 'Error while running .clickElement() protocol action: Appium error: An unknown server-side error occurred while processing the command. Original error: Did not get any response after 20s'.
2. I can use the XPath to locate the button on the pop-up dialogue and click it to close the dialogue, but it takes serval minutes, is there another way to do this operation more efficient?
const { client } = require('nightwatch-api')
const { Given, Then, When} = require('cucumber')
Given('open mobile 163 news', async function () {
await client.url('https://3g.163.com/news/article/EJN99AOF000189FH.html?clickfrom=index2018_news_newslist#offset=0')
})
When('choose share by QQ', async function () {
await client.waitForElementVisible('.sharelogo')
await client.click('.sharelogo')
})
Then('the popup should show', async function () {
await client.waitForElementVisible('.qzone')
await client.click('.qzone')
await client.setContext('NATIVE_APP')
await client.source(function(res){
console.log(res.value)
})
await client.useXpath()
await client.click('//*[#name="Allow"]')
await client.contexts(function(result) {
client.setContext(result.value[result.value.length - 1])
client.useCss()
})
})
Have you tried adding the capability 'nativeWebTap' and setting it to the value 'true' in the test scripts?

Interaction between restartBrowserBetweenTests with onPrepare()

I want each of my tests to run on clean browser (Firefox) so i use restartBrowserBetweenTests:true option. Because i use non-Angular demo app, in onPrepare() function i use browser.waitForAngularEnabled(false). It's works fine, when i run a single spec, but when i run 2 specs, i have error.
Angular could not be found on the page. If this is not an Angular application, you may need to turn off waiting for Angular.
How can i solve this? And in addition, how onPrepare works in this case - every time when browser starts or one time before all specs?
Here is my conf.js
const screenshotReporter = require('./screenshotCustomReporter')
exports.config = {
capabilities: {
browserName: 'firefox'
},
restartBrowserBetweenTests: true,
framework: 'jasmine',
directConnect: true,
baseUrl: URL,
specs: ['path/**/*Spec.js'],
// Options to be passed to Jasmine.
jasmineNodeOpts: {
defaultTimeoutInterval: 30000,
includeStackTrace: true
},
onPrepare: () => {
require("#babel/register");
jasmine.getEnv().addReporter(screenshotReporter)
browser.waitForAngularEnabled(false)
}
}
You can recreate this issue using the following simple project:
conf.js
exports.config = {
framework: 'jasmine',
specs: ['./app.1.js', './app.2.js'],
seleniumAddress: 'http://localhost:4444/wd/hub',
restartBrowserBetweenTests:true,
onPrepare:function(){
browser.waitForAngularEnabled(false);
},
}
app.1.js
describe('second test', () => {
it('should check is displayed successfully', () => {
browser.driver.get("https://stackoverflow.com");
browser.driver.sleep(5000);
expect(element(by.linkText('Ask Question')).isDisplayed()).toBe(true);
});
});
app.2.js
describe('first test', () => {
it('should check is displayed successfully', () => {
browser.driver.get("https://stackoverflow.com");
browser.driver.sleep(5000);
expect(element(by.linkText('Ask Question')).isDisplayed()).toBe(true);
});
});
OnPrepare is defined for all settings need to be executed for suite. It means it
is always one time operation irrespective of number of spec files.
One concept you need to understand is that whenever the new instance of
firefox browser is launched then WebdriverJs initialize the instance of webdriver.
and global object browser in protractor also gets initialized.
In your case First spec file start firefox browser, OnPrepare function is executed afterwards and
default setting of protractor is overriden by WaitForAngularEnabled.But when you run second spec file,
again firefox browser is launched with fresh instance of webdriver and protractor browser which expect
angular application and in that case test case gets failed.
The solution for this problem is to use before function in spec file
describe('first test', () => {
before(() => {
browser.waitForAngularEnabled(false);
});
it('should check is displayed successfully', () => {
browser.driver.get("https://stackoverflow.com");
browser.driver.sleep(5000);
expect(element(by.linkText('Ask Question')).isDisplayed()).toBe(true);
});
});
Note : If you are using restartBrowserBetweenTests: true then you will have to use beforeEach() function for waitForAngularEnabled because every time fresh instance of webdriver will be created.

Sendkeys not working in protractor because of "selenium webdriver promise defer error"

My scenario is to automate three sections in the same page and each section has a radio button, drop down etc..
After filling all the fields in each section I need to click Continue button and it takes a while to load.
I have successfully completed automating two sections, but when I click Continue button in the second section to navigate to the third. I am facing the below error
"TypeError: doneDeferred.fulfill is not a function"
I have understood that it may be synchronisation issue.So, I used
browser.ignoreSynchronisation=true;
Also, i tried to use
await browser.sleep(30000).then(function() {
console.log('Hello');
});
because I thought that since its taking a while to load its not inputting the values.But strangely, it doesn't go inside the function and print the console log message.
and this is the code in the debugger.js file:
validatePortAvailability_(port) {
if (this.debuggerValidated_) {
return selenium_webdriver_1.promise.when(false);
}
let doneDeferred = selenium_webdriver_1.promise.defer();
// Resolve doneDeferred if port is available.
let tester = net.connect({ port: port }, () => {
doneDeferred.reject('Port ' + port + ' is already in use. Please
specify ' +
'another port to debug.');
});
tester.once('error', (err) => {
if (err.code === 'ECONNREFUSED') {
tester
.once('close', () => {
doneDeferred.fulfill(true);
})
.end();
}
else {
doneDeferred.reject('Unexpected failure testing for port ' +
port + ': ' + JSON.stringify(err));
}
});
return doneDeferred.promise.then((firstTime) => {
this.debuggerValidated_ = true;
return firstTime;
}, (err) => {
console.error(err);
return process.exit(1);
});
Expected:I have pass values through sendkeys and drop down in that section and click continue button.
Actual:Its hanging in that screen and getting the above error.
I believe that the issue is with selenium webdriver promises.Can anyone help me with this?
I am still in the learning face with protractor cucumber framework.

In Nightwatch, `.url()` blocks assertions for too long

It appears that using browser.url() (http://nightwatchjs.org/api/#url) to navigate causes the WebDriver client to wait until the page is fully loaded - however I have assertions I want to make before that point.
"Shows splash screen for a few seconds": function(client) {
client
.url(client.testURL)
// at this point, the splash has already come and gone, so
// this next command times out
.waitForElementVisible('#splash img', 10000)
.waitForElementNotVisible('#splash', 10000);
},
Is this possible? I think my only other option is to disable the splash in a test scenario.
I'm running these tests on Firefox v45.
You can do this by setting Firefox profile preferences as shown in below
https://github.com/nightwatchjs/nightwatch/issues/748
The preference you need to set is webdriver.load.strategy to be unstable. But this means that waiting for page to load is now all your business
var FirefoxProfile = require('firefox-profile');
function setProfile(browser, profile, callback) {
profile.encoded(function (encodedProfile) {
browser.options.desiredCapabilities['firefox_profile'] = encodedProfile;
callback();
});
}
function setFirefoxProfile(browser, done) {
var profile = new FirefoxProfile();
profile.setPreference('webdriver.load.strategy', 'unstable');
setProfile(browser, profile, done);
}
// and in my test module
before: function (browser, done) {
setFirefoxProfile(browser, done);
}

Selenium Node API web driver wait timeout handler

I am new to Selenium web drive. Trying to do some page automation and are using driver.wait functions to wait for a selector rendering first and then do some operations later.
Was wondering if Selenium has a way to pass in a timeout handler to manage timeout if the element is not showing up after x seconds.
Here's my code:
driver.wait(function () {
return driver.isElementPresent(webdriver.By.css('input[id="searchMap"]'));
}, 10000);
So after 10 secs if input[id="searchMap"] does not show up, Selenium script will end and Error is thrown.
I am looking for something like this:
driver.wait(function () {
return driver.isElementPresent(webdriver.By.css('input[id="searchMap"]'));
}, 10000, function fail(){
console.log("Time is up!");
});
Found a solution myself. Have to use catch for Selenium promise class.
http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/promise_exports_Promise.html
Here is my code:
driver.wait(function () {
return driver.isElementPresent(webdriver.By.css('div.info-page'));
}, 10000).catch(function(e){
console.log('Catching Error');
});