waitForPopup in CasperJS always gets "wait timeout" - casperjs

In my page automation script, when I click a label link, a new window will be opened. The url of the link is generated by a complicated javascript.
<a class="link_text" href="#" onclick="process('2c913f9e4c7314e1014c74a4a5e02573')">办理任务 </a>
after triggered the url becomes:
https://oa.phicomm.com/workflow/doJob.action?taskVo.processExecutionId=2c913f9e49d0d5280149d12fabd90dd0&taskVo.taskId=10851011
I try to use the API- casper.waitForPopup after I do the click action.
casper.waitForPopup(/.+/, function(){
this.echo(this.getTitle());
}, null, 20000);
But it failed as wait timeout 20000ms expired. I also tried put the urlstring as the parameter:
casper.waitForPopup(/oa.phicomm.com\/workflow\/doJob\.action\?taskVo\.processExecutionId=2c913f9e49d0d5280149d12fabd90dd0&taskVo\.taskId=9100818/, function{
this.echo(this.getTitle());
});
still timeout. I also capture the screenshot, clearly it's still in the previous page. I try the thenOpen method directly inputting the url. It works. But since I want to auto it, I can't know the url before I actual trigger the link. I am a newer to CasperJS, anyone knows how to solve this problem? Thanks.

There was same issue with me i have resolve this problem by first selector exist or not
casper.[waitForSelector][1]('#example_id', function() {
this.echo("yes");
});
If your selector exist then try this
casper.[waitForPopup][1](0, function() {
this.echo('Popup');
});
casper.withPopup(0, function() {
this.capture('screen.png');
});
Read more about waitForPopup

Related

How to follow a list of links without id and name within CasperJS

The below page code has a list of links that have no id and name to be used for click on it:
My casperjs code is problematic since it needs an id or name:
...
casper.then(function() {
test.assertTextExists("User Data", "Login ok");
test.clickLabel('Link_for_Data_1','a');
});
...
Also by using the xpath with href seems not to work:
this.click(x('//*[#href="#cookpot-home/data-cookpot"]/a[text()="Link_for_Data_1"]'));
Some more page code, maybe it helps (the dark blue in the below picture is the part which has the link, see the first picture):
After some investigation I realized that it may need some more time to load the next page after the login to this page.
I added a wait timer for 10s but it still didn't helped!
...
casper.then(function() {
casper.wait(10000, function() {
this.echo('should appear after 10s');
});
});
casper.then(function() {
console.log(this.getCurrentUrl());
casper.waitForText('Link_for_Data_1', function() {
this.echo('FOUND');
});
test.assertTextExists("Data page", "Login into Data");
...
The above code seems to work until it comes to the waitForText which never prints 'FOUND'.
Also the assertTextExists fails, but according to the getCurrentUrl it is in the correct page (the page shows the text "Data page")!!!
So all the above seems to be a step to the right way but it still didn't answers my main question.

How to get url of next page of clicking submit button in Protractor?

I am able to run the test code and i am able to submit the form. I need the url of next page when submit button is click. I need to get url of next page when submit button is clicked. But i am getting curenturl of first page.
My code:
describe('Pmts app 5.0',function(){
var LoginPage = require("./LoginPO");
it('login test', function(){
LoginPage.setUserName('xyz#test.com');
LoginPage.setPassWord('test');
LoginPage.clickButton();
expect(browser.getCurrentUrl()).toEqual('https://localhost:1234/#/timesheet');
});
});
After submit button is clicked browser get closed immediately and it is not waiting for next page to load. Once the button is submitted it should wait until next page load and I need url of next page...
Put it in the following way:
LoginPage.clickButton().then(function(){ expect(browser.getCurrentUrl()).toEqual('https://localhost:1234/#/timesheet');
})
or you can put a explicit hard sleep:
browser.sleep(time in ms)
or you can put a browser.wait() to wait for certain element that appears on the next page:
browser.wait(function(){
return elem.isPresent()
},time in ms).then(function(){
expect(browser.getCurrentUrl())
.toEqual('https://localhost:1234/#/timesheet');
})
Please refrain from using browser.sleep(number), instead go for browser.waitForAngular() if your application is Angular.js based.
Using hard coded dependency is never a good idea.
I have noticed, that the time Protractor needs to load the whole page varies. Therefore, either use browser.waitForAngular() or set a high number in browser.sleep()
Try this :
describe('Pmts app 5.0',function(){
ptor = protractor.getInstance();
var LoginPage = require("./LoginPO");
it('login test', function(){
LoginPage.setUserName('xyz#test.com');
LoginPage.setPassWord('test');
LoginPage.clickButton();
ptor.sleep(1000);
expect(browser.getCurrentUrl()).toEqual('https://localhost:1234/#/timesheet');
});
});

CasperJS form fill sometimes stays on the current page

I have a simple casperjs test to submit a search form on my homepage. Then I assert that the title on the landing page is correct.
Works fine on my computer (OSX 10.9.2) but on my colleague's laptops (a Win 7 and Win 8), the test fails randomly because casper "thinks" it is still on the search page.
casper.test.begin('Search', function(test) {
casper.start("http://localhost:8080/site", function() {
this.fill(searchForm, { query: goodQuery }, true);
});
casper.then(function() {
// sometimes fails, says it's "My Project" main title
test.assertTitle('Search Result', 'Search result title is ok');
});
}
Introducing a casper.waitFor(3000) before checking the page title does not change the outcome. I've also tried to replace the then step by a waitForUrl, but it fails after 5 secs, saying it is still on the current page.
Plenty of other tests work fine on all computers but it's the only one with form submition.
Any hints on how to solve or properly work around this? I'd rather not simulate a click on the submit button (more coupling to the form internals) if possible (but it would be okay I guess).
Thanks
$ casperjs --version
1.1.0-beta3
$ phantomjs --version
1.9.7
EDIT: submitting the form and waitForUrldid not help. I found out that the test actually runs fine on its own, even on the Windows 7 machine. But when I run two tests:
01 search.js (the one described above)
02 menu.js (a simple one, merely containing assertExists)
'search.js' fails most of the time... and sometimes 'menu.js' fails instead! I suspect some mishandled concurrent access, although it consistently works on OSX. I must be doing something wrong. Both tests have the same structure:
casper.test.begin('Some test', function(test) {
casper.start(someUrl, function() {
// some test
});
casper.run(function() {
test.done();
});
});
Any clue?
Try :
casper.test.begin('Search', function(test) {
casper.start("http://localhost:8080/site", function() {
this.fill(searchForm, {
query: goodQuery
},false);
this.click("your selector for submit button");
});
casper.then(function() {//you should use waitForUrl/Selector/Text() instead
// sometimes fails, says it's "My Project" main title
test.assertTitle('Search Result', 'Search result title is ok');
});
casper.run(function() {
this.test.comment('------ Tests over ----\n');
test.done();
});
});
It's better to submit the form by clicking. Sometimes (often) it doesn't pass putting the fill arg at true. Just put the correct selector for the submit button.
You should wait for an item to appear on the following page. I would change your code to the following:
casper.test.begin('Search', function(test) {
casper.start("http://localhost:8080/site", function() {
this.fill(searchForm, { query: goodQuery }, true);
});
casper.waitForSelector('#someSelectorOnNextPage', function() {
test.assertTitle('Search Result', 'Search result title is ok');
});
}
I also experience same issue. Suprisingly adding empty then() handler fixes that in v1.1.0-beta3. I don't think this is expected behavior though:
casper.test.begin('Search', function(test) {
casper.start("http://localhost:8080/site", function() {
this.fill(searchForm, { query: goodQuery }, true);
});
// Do nothing here, just call it as a placeholder
// Here http://localhost:8080/site sends us to the next endpoint
casper.then(function() {});
// Now this is the final page we actually want to assert
casper.then(function() {
test.assertTitle('Search Result', 'Search result title is ok');
});
}
EDIT:
Although question author says casper.waitForUrl() didn't work for them, it did work for me as an alternative solution.
What does look strange is that in verbose mode whatever returns a 301 status code along with Location Header is recognized as HTTP 200 response by Casper.
EDIT 2:
Well obviously it doesn't happen every time, but what I noticed is that Casper sometimes doubles the previous response (that's why I thought it recognizes some specific HTTP codes as 200 mistakenly and that's why author's code functioned as if it stayed on same page after form submission) and sometimes not.
waitForUrl() fixes that obviously but there is still some underneath issue in Casper which scares me a bit and I hope I will find some time to report it with all the dumps to Casper issue tracker.

jQuery get() not triggering on click, no errors in console

I'm banging my head against the wall here, and hoping I'm just blind to something obvious.
I have this DOM structure:
<ul>
<li>
<h3>Lorem ipusm</h3>
<p class="remove-story">Delete</p>
</li>
</ul>
And this is my jQuery:
$(".remove-story a").click(function()
{
var parent = $(this).closest('li');
$.get($(this).attr('href'), function()
{
$(parent).fadeOut();
});
return false;
});
As it stands, clicking on the link within .remove-story does nothing, and the action triggered by the URL in the link does not occur either.
No JS errors pop-up in the console either on page load or when clicking the link.
If I remove the $.get function and simply fade out the list item, that works as expected.
If I visit the URL manually (or remove the return false and click the link), the link works and the back-end action completes (story removed).
Is there an error in my code here that anyone can spot? If not, any ideas as to where to look next to troubleshoot this?
I would firstly check if that get request really been send our, I mean using some tool like Fiddler or Chrome developer tool network tab, and then maybe add break point inside the call see if the parent you in that certain context is nothing or a wrong object
Wrap your onclick with $(document).ready(function() {}):
$(".remove-story > a").click(function(e)
{
var parent = $(this).closest('li');
alert($(this).attr('href'));
$.ajax({
url: $(this).attr('href'),
success: function() {
alert(12);
$(parent).fadeOut();
},
error: function(e) {
alert('baaaahhhh:' + e);
}
})
//e.stopPropagation();
return false;
});
​
Update: Changed $.get to $.ajax to see if thrs error while doing Ajax request.
You need to encode your link encodeURIComponent($(this).attr('href'));
Here is a jsfiddle working:
http://jsfiddle.net/9Wnt8/

JQuery post not working in document but is working in console?

I have a page which needs to use $.post() but for some reason the exact code works when I run it from the firebug console but not from my script? My script is:
$(document).ready(function () {
$('#dl_btn').click(function () {
$.post(
"news_csv.php",
$("#dl_form").serialize(), function (data) {
alert('error');
if (data == 'success') {
alert('Thanks for signing up to our newsletter');
window.open("<?php echo $_GET['link']; ?>");
parent.Shadowbox.close();
} else {
alert(data);
}
});
});
});
It isn't the link as that does get printed properly but it gives me an error on line 140 of jquery min, I have tried using different versions of jquery and to no avail. I really dont understand why this isn't working.
When I changed from $.post to $.ajax and used the error callback I did receive an error of 'error' and the error is undefined?
Don't suppose anyone has any ideas? Would be much appreciated.
Thanks,
Tom
Is your click button placed inside a form element?
Cause doing so, clicking on it will not only trigger the onClick event you have binded to, but form submit as well, so you will end up in a case where your browser is executing both requests in parallel - with unpredicted outcome, of course.
I tried the same code with an element that does not trigger form submit and it worked as expected.
One point though: if you plan to use simple string as a return value and to do something with it (display it or so) then is ok to do what you do right now. However, if you have more complex response from the ajax request, you should specify the response type (xml, json..) as the last parameter of the post method.
Hope this helps.

Resources