Some protractor tests are breaking when I include a comment beside them or a
.then(() => console.log("hello")) statement beside a promise element.
Works:
menuIcon.click().then(function () { expect(isWorking).toBeTruthy(); });
Doesn't work:
menuIcon.click().then(function () { expect(isWorking).toBeTruthy(); }).then(() => console.log('Verified menu is working');
Error:
Failed: stale element reference: element is not attached to the page document
However, using different text for the comment makes this work such as:
menuIcon.click().then(function () { expect(isWorking).toBeTruthy(); }).then(() => console.log('comment');
Why might this be? Without them it always passes.
Thanks :)
Related
I've come across a use of functions in JavaScript that I can't seem to convert, correctly, to CoffeeScript syntax. I'd appreciate it if someone could let me know how to do this:
JavaScript version:
node.addEventListener('mousedown', () => {
setTimeout(() => {
console.log('pressed');
return;
}, 3000);
});
The function setTimeout expects 2 parameters - a function and a duration in milliseconds.
Here's my first attempt at converting to CoffeeScript:
node.addEventListener 'mousedown', () =>
setTimeout () =>
console.log 'pressed'
return, 3000
return
This just gives me an error "unexpected comma" on line 4. So, I tried putting the duration on a separate line but, depending on whether I indent it one level or two, it just becomes part of the function code of either the function being passed to node.addEventListener (2nd parameter) or the function being passed to setTimeout (1st parameter).
You need to move both the comma and 3000 to a new line. The following code:
node.addEventListener 'mousedown', () =>
setTimeout () =>
console.log 'pressed'
return
, 3000
return
compiles to
node.addEventListener('mousedown', () => {
setTimeout(() => {
console.log('pressed');
}, 3000);
});
Tested here.
Another option is to wrap the inner function in parentheses:
node.addEventListener 'mousedown', () =>
setTimeout (() =>
console.log 'pressed'
return), 3000
return
My protractor script is working fine until a page where reveal.js package is used. I am not sure if that is the reason it causes the scripts to fail, but otherwise the code base is same as the other pages where my scripts works fine.
Note: I tried most of the protractor actions (click, highlight, waitForElement, toContain, etc), none of them worked. I could only click the links by inserting jQuery in my script.
CODE:
let HighlightElement = function (el) {
return browser.executeScript("arguments[0].setAttribute('style', arguments[1]);", el.getWebElement(), "color: Red; border: 1px solid red;").
then(function (resp) {
browser.sleep(2000);
return el;
}, function (err) { });
}
let waitUntilElementPresent = function (visibilityOfObject, maxWaitTime) {
var EC = protractor.ExpectedConditions;
browser.manage().timeouts().implicitlyWait(2);
browser.wait(function () {
browser.manage().timeouts().implicitlyWait(3);
return visibilityOfObject.isDisplayed()
.then(
function (isDisplayed) {
browser.wait(EC.visibilityOf(visibilityOfObject), maxWaitTime, "Element taking more time to load");
browser.manage().timeouts().implicitlyWait(3);
return isDisplayed;
},
function (error) {
return false;
});
}, 100000);
}
ACTUAL CODE:
var homepage = new homePageObj();
utilities.waitUntilElementPresent(homepage.waitScreenText); //here the script is failing. It is just a simple script and it used to work in other pages but it doesn’t work only in some of the pages
utilities.HighlightElement(homepage.waitScreenText);
utilities.HighlightElement(homepage.startButton);
homepage.startButton.click();
Error:
Failed: Wait timed out after 120062ms
Using below jQuery I am able to click, but we need to give wait time explicitly for each and every java script that we use, which is time consuming. I am beginner in automation, Kindly help me with a solution.
browser.executeScript("document.getElementsByClassName('cc-button')[0].click()");
My code started running after proving "browser.waitForAngularEnabled(false)" at the start of the script.
I want to store in variable the path of local directory selected using dialog.showOpenDialog.
I tried every tutorial and stack answer without any luck.
Best part is that even the electron-api-demos solution isn't working (I am using one here)
So, dialog opens, I can select a folder or a file - but nothing is send back to renderer process.
Here I am testing this with opening files but also no results.
I am using jquery btw and elcetron v 12.
Here is my code:
Renderer process:
$('.js_select_folder').on('click', (e) =>{
ipcRenderer.send('open-folder')
e.preventDefault();
})
ipcRenderer.on('selected-folder', (event, path) => {
console.log('wtf',path)
$('#info').text('Result :'+path)
})
Main process:
ipcMain.on('open-folder', (event) => {
dialog.showOpenDialog({ properties:['openFile']}, (files) => {
if(files){
console.log(files)
event.reply('selected-folder', files)
}
})
})
No trace of console.log(files) anywhere.
Any ideas what I am doing wrong?
I figured it out:
renderer process:
$('.js_test').on('click', (e)=>{
e.preventDefault();
ipcRenderer.send('open-folder')
})
ipcRenderer.on('sel-dir', (event, path) => {
const message = `This app is located at: ${path}`
$('#info').text(message)
})
main process:
ipcMain.on('open-folder', (event, arg) => {
dialog.showOpenDialog({properties: ['openDirectory']}).then(result=>{
event.sender.send('sel-dir', result.filePaths)
})
})
So it is actually super simple as you can see but took me two days to figure it out.
The result is that I get the path (or any data) back to renderer after choosing dir or file.
Regards, Mac.
I have a repeated set of test cases to be used for different users to check one after the other. How do I do this? I thought of having a function and calling it twice when needed but in that case it will be kinda nested it so it wouldn't work. Can some one suggest me a solution?
Encapsulating and re-using code is exactly what you want to do here. I recommend you have a look at the Page Object pattern too.
Take, for example, an application generated with Angular CLI (just ng new really):
describe('ngrx-intro App', () => {
let page: AppPage;
beforeEach(() => {
page = new AppPage();
});
it('should display welcome message', () => {
page.navigateTo();
expect(page.getParagraphText()).toEqual('Welcome to app!');
});
});
export class AppPage {
navigateTo() {
return browser.get('/');
}
getParagraphText() {
return element(by.css('app-root h1')).getText();
}
}
Based on this example, or your code, you can compose your own test code which you can re-use for multiple different users.
If I have understood the question correctly , #Shrinidhi , wants to implement a data driven test using jasmine.
This can be achieved using the jasmine-data-provider npm package.
The demo code from the page it self looks something like this :
var using = require('jasmine-data-provider');
describe('test subtraction with data provider - direct array', function () {
using([{a: 5, b: 2, expected: 3}, {a: 25, b: 26, expected: -1}], function (data) {
it('should calc with operator -', function () {
var result = calculator.calc(data.a, data.b, '-');
expect(result).toEqual(data.expected);
});
});
});
I am not able to redirect on required page on sigIn page. It clicks on button but not giving the output as required. Not able to hit the server for further process after signin process.Plz help
describe("it should be able to run on different events as defined", function(){
it("should be get on browser", function(){
browser.get("http://www.localhost:8100/#/signin");
expect(browser.getCurrentUrl()).toEqual("http://www.localhost:8100/#/signin");
browser.sleep(2000);
element(by.css("[ng-model='user.email']")).sendKeys('ash.mat23.23#gmail.com');
element(by.css("[ng-model='user.email']")).getAttribute('placeholder').then(function(element){
expect(element).toEqual('Email');
browser.sleep(2000);
});
element(by.css("[ng-model='user.password']")).sendKeys('123456');
element(by.css("[ng-model='user.password']")).getAttribute('placeholder').then(function(element){
expect(element).toEqual('Password (at least 6 characters)');
browser.sleep(2000);
});
element(by.id('signin_submit_btn'));
browser.driver.actions().sendKeys(protractor.Key.ENTER).perform();
browser.sleep(5000);
});
});
Are you sure it's clicking the button properly? To my mind the line that reads:
element(by.id('signin_submit_btn'));
Should have a click event on the end, i.e. it should read:
element(by.id('signin_submit_btn')).click();
I'm not completely sure what you are asking. It looks to me that you are preforming all actions at the same time. You should make sure filling in the username and password is done before clicking on submit or pressing enter. I hope this helps:
describe("it should be able to run on different events as defined", function () {
it("should be get on browser", function () {
browser.get("http://www.localhost:8100/#/signin").then(() => {
expect(browser.getCurrentUrl()).toEqual("http://www.localhost:8100/#/signin");
Promise.all([
$("[ng-model='user.email']").sendKeys('ash.mat23.23#gmail.com'),
$("[ng-model='user.password']").sendKeys('123456'),
$("[ng-model='user.email']").getAttribute('placeholder').then(function (element) {
expect(element).toEqual('Email');
}),
$("[ng-model='user.password']").getAttribute('placeholder').then(function (element) {
expect(element).toEqual('Password (at least 6 characters)');
})
]).then(()=>{
$('#signin_submit_btn').click(); // or browser.driver.actions().sendKeys(protractor.Key.ENTER).perform();
})
})
});
});