wait for complete page load does not work as expected in Protractor - protractor

I am writing a test for login into non-angular application.
describe('login Test for App', function () {
beforeEach(function () {
browser.ignoreSynchronization=true;
browser.get(loginPageURL,2000);
});
it('It should Login a User', function () {
element(by.id('username')).sendKeys(constants.userName);
element(by.id('password')).sendKeys(constants.password);
element(by.id('Login')).click().then(function () {
// Waiting to open modal
browser.wait(function () {
return browser.getCurrentUrl().then(function (url) {
return url==dashboardUrl;
});
});
});
});
});
After login, I want to check currentUrl. But After login button click, It waits till dashboard url appeared.
But when after loginbutton click, It go to diffrent url, Then it also wait for dashboard url infinite.
I want to check current url after login event, if it is not dashboard url, then it should fail, and do not run next test suite cases, because login Test is failed.
Like-
When clickOn login button.
Wait for complete page load.
2.Then check current url, If it is not dashboard url, then test should failed, and not proceed for any test cases.

It is not best practice to wait for the pages url to load in order to know if the page is loaded, because there could be redirects or other things.
It is best practice to wait for a specific element on the next page(after log-in). Here is your code refactored to use a custom wait() function that will wait for an element to appear before continuing to retrieve the current url:
describe('login Test for App', function () {
browser.ignoreSynchronization = true;
it('should load the log-in page', function(done) {
browser.driver.get(loginPageURL).then(function() {
browser.driver.sleep(2000);
wait('#username', 10000);
done();
});
});
it('It should Login a User', function (done) {
element(by.id('username')).sendKeys(constants.userName);
element(by.id('password')).sendKeys(constants.password);
element(by.id('Login')).click().then(function () {
// Waiting to open modal
wait('#element_on_the_next_page', 10000);
browser.getCurrentUrl().then(function (url) {
// do anything with the url
done();
});
});
});
function wait(selector, timeout) {
browser.driver.wait(function() {
return browser.driver.isElementPresent(by.css(selector)).then(function(present) {
return present;
});
}, timeout);
browser.driver.wait(function() {
return browser.driver.findElement(by.css(selector)).isDisplayed().then(function(displayed) {
return displayed;
});
}, timeout).then(function() {
return;
});
}
});
Hope this helps!

Related

How to implement Authentication using Riverpod in Flutter?

I have three providers for login => email, password, login.
then I am refreshing login provider which is a future provider and will call the login API.
now I need to see if the provider returns success then I will need to navigate to next page.
But .when() is not waiting for the refreshing to finish. Its logging "loading..." in the console.
`
ref.watch(email.notifier).state =
emails.trim().toLowerCase();
ref.watch(password.notifier).state = passwords;
final loginstate = ref.refresh(login);
loginstate.when(data: ((data) {
// log(data + "here");
if (data == "Success") {
context.go('/home');
}
}), error: (error, stackTrace) {
// log(error.toString() + "here");
}, loading: () {
log("loading....");
});
`
I need to Navigate to the Home page upon successful API call.

next-auth pass the data in signin callback

I am making login process with next-auth.
I am using Google as OAuth Provider.
// pages/api/auth/[...nextauth].ts
export default async function auth(req: any, res: any) {
return await NextAuth(req, res, {
//...
callbacks: {
async signIn({ user, account }) {
// console.log(user); // data exists.
const isUser = await apiForIsUser();
if(isUser) return true; // redirect to root page.
return '/sign-up'; // redriect to sign-up page.
}
}
});
}
above code works, but I can not touch data from OAuthProvider in sign-up page.
How can I pass the data from 'OAuthProvidertosign-up` page?

Paypal javascript sdk onClick not prevent popup showing

I follow this documentation to integrate paypal javascript sdk. I want validate user input after paypal button click and prevent paypal window display if validation fail.
paypal.Buttons({
onClick: function(data, actions) {
console.log('click >>>>>')
return actions.reject();
// return false;
},
createOrder: function(data, actions) {
return fetch('/checkout/submit/paypal/create_order', {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(_this.getSubmitData())
}).then(function(res) {
return res.json();
}).then(function(detial) {
return detial.orderId
});
},
onApprove: function(data, actions) {
// data > { billingToken, facilitatorAccessToken, orderID, payerID, paymentID }
return fetch('/checkout/submit/paypal/capture_order?orderId=' + data.orderID, {
method: 'post',
headers: {
'content-type': 'application/json'
},
}).then(function(res) {
return res.json();
}).then(function(details) {
console.log(details);
})
}
}).render('#paypal-button-container');
When i click the paypal button, console show my message click >>>>> and a new window display and disappear immeditely.
I don't think it should show a new window.
I don't think it should show a new window.
Nonetheless, that's how it works if you reject from onClick.
To prevent the popup from opening, you need to disable the buttons in onInit and add a listener for re-enabling them when appropriate, as documented in the link in your question.

Loading controller await problem with Ionic

In my ngOnInit page I call a function to present a Loading and then call my service to load data.
The problem is that the service call ends before the loading starts.
How is it possibile with await?
loading: any;
...
ngOnInit() {
this.presentLoading();
this.myService.get().subscribe((item)=>{
console.log('dismiss');
this.loading.dismiss();
}, ()=>{
this.loading.dismiss();
})
}
async presentLoading() {
this.loading = await this.loadingController.create({
message: 'Loading...',
duration: 20000
});
console.log('presentLoading')
await this.loading.present();
}
In console i can see:
dismiss
ERROR TypeError: Cannot read property 'dismiss' of undefined....
presentLoading
I’m using Ionic4.
In your case (based on the code) you have presentLoading function that returns a promise (async), so even though inside of it you are using await statements - inside the ngOnInit hook nothing instructs execution to await for the presentLoading.
You could do:
async ngOnInit() {
await this.presentLoading();
this.myService.get().subscribe((item)=>{
console.log('dismiss');
this.loading.dismiss();
}, ()=>{
this.loading.dismiss();
})
}
Also since you are using Onservables it may makes sense to call dismiss inside "completed" function:
async ngOnInit() {
await this.presentLoading();
this.myService.get().subscribe((item)=>{
console.log('dismiss');
}, (error)=>{
console.log(error);
}, (completed)=>{
this.loading.dismiss();
}
})
A note - making ngOnInit an async method does not "hurt", but its best to know a bit more waht implications it has: async/await in Angular `ngOnInit`

Protractor open new browser for each test

I want to open new browser for each new test starting at 'describe(info[i].TestName'. Both Login and Start App are part of one test.
Right now both tests run in same browser and then browser is closed.
for (let i in info) {
describe(info[i].TestName, () => {
beforeAll(function () {
browser.get('http://abcsite.com');
});
describe('Login', () => {
Login.start();
});
describe('LibraryApp', () => {
LibraryApp.app(info[i]);
});
});
}