Avoid one test out of multiple test files in firefox protractor - javascript

I have multiple tests in my tests folder where the naming conventions for all the tests ends with spec.js. I am running all the tests from the Config file with */spec.js option.
I want to skip running one test in FF as it is not supported in that browser. This is what I am attempting to do but it is not skipping that tests. Please advise.
multiCapabilities: [{
'browserName': 'chrome',
'chromeOptions' : {
args: ['--window-size=900,900']
// }
},
},
{
'browserName': 'firefox',
'chromeOptions' : {
args: ['--window-size=900,900']
// }
},
}],
specs: [
'../tests/*.spec.js'
],
I have the following in my onPrepare function:
browser.getCapabilities().then(function (cap) {
browser.browserName = cap.caps_.browserName;
});
In one of the test file where I am looking to skip running this test in FF, I am doing this
if(browser.browserName=='firefox') {
console.log("firefox cannot run *** tests")
} else {
blah... rest of the tests which I want to execute for Chrome and IE I have put it in this block}
But still the test which I wanted to skip running in FF still runs.
Please advise.

An easy way to do this is to update your firefox multicapabilities to exclude particular test spec using exclude tag. This prevents use of an if condition and additional lines of code. More details are here. Here's how -
multiCapabilities: [{
browserName: 'chrome',
chromeOptions : {
args: ['--window-size=900,900']
},
},
{
browserName: 'firefox',
// Spec files to be excluded on this capability only.
exclude: ['spec/doNotRunInChromeSpec.js'], //YOUR SPEC NAME THAT YOU WANT TO EXCLUDE/SKIP
}],
Hope it helps.

As soon as browser.getCapabilities() is asynchronous and is based on Promises, your code inside .then() may be executed later than the rest of the code. I guess your if condition is placed inside describe block, which actually runs before the value for browser.browserName is set, as a result you get a value of undefined for it and condition fails. To make sure that your tests run after all the preparations are done, you should return a promise from onPrepare:
onPrepare: function() {
return browser.getCapabilities().then(function (cap) {
browser.browserName = cap.caps_.browserName;
});
}
Protractor will explicilty wait until it resolves and then start executing the tests.
describe('Suite', function () {
console.log(browser.browserName); // 'firefox'
it('spec', function () {
expect(true).toBe(true);
});
});

Related

How to add a screenshot on a test failure using Appium and WebdriverIO

I currently have multiple wdio.config files due to the multiple apps in my end to end suite
it looks like this and all these files have the allure reporting command in it and my reports are working fine:
every file has an allure reporting command like this:
reporters: [['allure', {
outputDir: 'allure-results',
disableWebdriverStepsReporting: true,
disableWebdriverScreenshotsReporting: true,
}]],
and also the desired capabilities like this:
capabilities: {
App: {
port: 4723,
capabilities: {
platformName: 'iOS',
'appium:platformVersion': '13.6',
'appium:orientation': 'PORTRAIT',
'appium:noReset': true,
'appium:newCommandTimeout': 240,
"appium:platformName": "iOS",
"appium:deviceName": "iPhone 8",
"appium:bundleId": "com.app",
}
},
},
and i also have a separate general wdio file which a general file and have not added much in it.
afterStep: function (test, context, { error, result, duration, passed, retries }) {
if (error) {
browser.takeScreenshot();
}
}
I have tried adding the after hook in the custom wdio config file as well but i am not able to have the screenshot on failure. i have also used App.takeScreenshot(); command as well instead of using browser.takeScreenshot(); but no luck.
Not sure about appium part but below code as you also have used in wdio.conf.js (or may be custom wdio file) works in webdriverio. I have used it in my framework.
afterTest: function(test, context, { error, result, duration, passed, retries }) {
if (!passed) {
browser.takeScreenshot();
}
},
This should work:
afterTest: async function (test) {
browser.saveScreenshot("browserfull.png")
// OR
driver.saveScreenshot("driverfull.png")
}

Protractor / Jasmine timing out on headless chromium - Error: Timeout - Async callback

Hi all so I am trying to run headless chromium with protractor and jasmine. I have everything setup and working for both firefox and chrome with a head. When I run firefox headless it works... when I try running chromium headless it ends up timing out. Looking for some help to solve this problem.
The error I get is :
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Now I've read a million article online and have tried increasing the timeout time and adding done into the function as well...
Here is my current code:
Conf.js - this has a bunch of added args and settings that I've found online. I've tried pretty much every variation and had no success..
exports.config = {
framework: 'jasmine2',
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['spec.js'],
allScriptsTimeout: 5000000,
capabilities: {
'directConnect': true,
'browserName': 'chrome',
"goog:chromeOptions": {
args: ["--headless", "--remote-debugging-port=9222", "--verbose", "--disable-gpu", "--disable-web-security", "--window-size=800x600"],
'binary': "/usr/bin/chromium-browser"
}
}
};
Spec.js - straight off of their website with console.logs. All of the console.logs print out in the following order 3,1,2. This is something I'm unsure is correct? Should the describe be waiting for the it to finish? It almost feels like my it is never returning...
describe('angularjs homepage todo list', function() {
it('should add a todo', function(done) {
console.log("WOOO1");
browser.get('https://angularjs.org');
element(by.model('todoList.todoText')).sendKeys('write first protractor test');
element(by.css('[value="add"]')).click();
var todoList = element.all(by.repeater('todo in todoList.todos'));
expect(todoList.count()).toEqual(3);
expect(todoList.get(2).getText()).toEqual('write first protractor test');
// You wrote your first test, cross it off the list
todoList.get(2).element(by.css('input')).click();
var completedAmount = element.all(by.css('.done-true'));
expect(completedAmount.count()).toEqual(2);
console.log("WOO2");
}, 15000);
console.log("WOO3");
});
Following this some other discoveries I have found... when I go to the localhost:9222 I see
Inspectable WebContents
data:text/html,<html></html>
The data:text/html,<html></html> is a link and if clicked on takes me to the remote chrome debugger that is loading ... data:text/html,. This is where I think the problem is. Why is this never actually loading the angular site?
Maybe I'm off base but does anyone know how to make sense of this?
EDIT:
Additional useful information.
I am using
chromium 79.0.3945.130
chromedriver 79.0.3945.36
jasmine v3.5.0
jasmine-core v3.5.0
Protractor 5.4.3
Thanks
Config that ended up working for me
exports.config = {
framework: 'jasmine',
allScriptsTimeout: 9000,
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['spec.js'],
capabilities: {
'directConnect': true,
'browserName': 'chrome',
"goog:chromeOptions": {
args: ["--headless", "--remote-debugging-port=9222", "--verbose", "--disable-gpu", "--disable-web-security", "--window-size=800x600"],
'binary': "path to chrome"
}
}
};
add allScriptTimeOut in your configuration like this:
exports.config = {
capabilities: {
'browserName': 'chrome'
},
framework: 'jasmine',
specs: ['example_spec.js'],
allScriptsTimeout: 9000
};
hope this link will help you
https://www.protractortest.org/#/timeouts.

multiCapabilities and jasmine focused tests

The Story:
We have a rather huge end-to-end protractor test codebase. We have two configs - one is "local" - to run the tests in Chrome and Firefox using directConnect, and the other one is "remote" - to run tests on a remote selenium server - BrowserStack in our case.
Our "local" config is configured to run some tests in Chrome and some in Firefox - because we really cannot run some tests in Chrome - for instance, keyboard shortcuts don't work in Chrome+Mac. Running the tests that require using keyboard shortcuts in Firefox is a workaround until the linked chromedriver issue is resolved.
Here is the relevant part of the configuration:
var firefox_only_specs = [
"../specs/some_spec1.js",
"../specs/some_spec2.js",
"../specs/some_spec3.js"
];
exports.config = {
directConnect: true,
multiCapabilities: [
{
browserName: "chrome",
chromeOptions: {
args: ["incognito", "disable-extensions", "start-maximized"]
},
specs: [
"../specs/**/*.spec.js",
"../specs/**/**/*.spec.js",
"../specs/**/**/**/*.spec.js"
],
exclude: firefox_only_specs
},
{
browserName: "firefox",
specs: firefox_only_specs
}
],
// ...
};
The problem:
Now, the problem is that, if I'm debugging a single test, or want to run a single test - I'm marking it is as focused (via fdescribe/fit) - but protractor starts two driver sessions - one for Chrome and the other one for Firefox, using both configured capabilities:
Running "protractor:local" (protractor) task
[launcher] Running 2 instances of WebDriver
...
------------------------------------
[chrome #1] PID: 2329
[chrome #1] Using ChromeDriver directly...
[chrome #1] Spec started
...
------------------------------------
[firefox #2] PID: 2330
[firefox #2] Using FirefoxDriver directly...
[firefox #2] Spec started
...
The question:
Is there a way to tell protractor to use the only one capability that has a focused spec configured?
Using currently latest protractor 3.0.0.
Hope the question is clear. Let me know if you need any additional information.
I wonder if you can do something to wrap the it statements like:
onPrepare: function() {
browser.getCapabilities().then(function(caps) {
global.browserName = caps.caps_.browserName;
});
global.firefoxOnly = function(name, testFunction) {
if (browserName === 'firefox') {
return it(name, testFunction);
} else {
return xit(name, testFunction).pend('firefox only');
}
};
}
Then when you write a test, instead of it use something like:
describe('when I do something', function() {
firefoxOnly('it should do the right thing', function() {
doSomething();
expect(thing).toBe(right);
)};
});
I have no idea if this actually works, just throwing it out there. In fact, when I get back to my testing computer and try it out, I would be interested in adding a function like wip to use instead of xit to automatically pend my ATDD tests!
Is there a way to tell protractor to use the only one capability that has a focused spec configured?
According to the relevant github issue, it is not possible.

Tests result as successful, although errors are found during Tests

I have set up my test environment as described here with QunitJS + PhantomJS + GruntJS: http://jordankasper.com/blog/2013/04/automated-javascript-tests-using-grunt-phantomjs-and-qunit/
Everything works fine, but I have the problem that, my Grunt Task finishes without errors, although errors are found. This is crucial for my build process. Due to the Test results the build either fails or succeeds. But in my case the build always succeeds. Any Ideas why grunt doesn't exit with failure when errors found?
qunit Task of the grunt file:
module.exports = {
services: {
options: {
urls: [
'http://localhost:8000/tests/services.html'
],
timeout: 20000,
force: true
}
},
gui: {
options: {
urls: [
'http://localhost:8000/tests/gui.html'
],
timeout: 20000,
force: true
}
}
};
Output:
Please consider that I cant upload more info due to confidental issues.
You are asking 'why does Grunt continue and when the tests fail?' The answer is 'because you are asking it to'.
The force option controls whether the QUnit task fails if there are failing tests. Setting it to true as you have done tells Grunt to continue even if there are failing tests. Try setting it to false, or removing it altogether as false is the default.

AngularJS: e2e testing with protractor

First of all I'm a noob with e2e testing. What I've done is
installed protractor nmp install protractor
installed webdriver-manager
run webdriver-manager start from the directory where my angularjs app sits. it runs fine
run protractor tests/e2e/conf.js it also runs fine
However in few seconds it says Timed out waiting for page to load
Here are my files:
tests/e2e/conf.js:
// An example configuration file.
exports.config = {
// The address of a running selenium server.
seleniumAddress: 'http://localhost:4444/wd/hub',
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome'
},
// Spec patterns are relative to the current working directly when
// protractor is called.
specs: ['example_spec.js'],
// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000
}
};
tests/e2e/example_spec.js
var protr;
describe('addressBook homepage', function() {
var ptor;
beforeEach(function() {
ptor = protractor.getInstance();
});
it('should greet the named user', function() {
browser.get('/'); // <-- exception here
element(by.model('yourName')).sendKeys('Julie');
var greeting = element(by.binding('yourName'));
expect(greeting.getText()).toEqual('Hello Julie!');
});
});
I just can't understand where to define my webapp laypout/placement so protractor/webdriver knows which context to run.
With that setup protractor will be trying to navigate to "/". You need to either set the baseUrl property in the configuration file, or put an absolute url in the browser.get(). If all your tests are going to be on the same domain I'd recommend the first option.

Categories

Resources