Intellij : Geb page object pattern navigation - intellij-idea

While developing Geb script in IntelliJ it's very hard to navigate to page object method, So every time it required to open that page and serach for method.
For example:
Class LoginPage extends Page {
static at = { waitFor("slow") { title == "Login Page" }}
static content = {
txtUsername {$("#txtUserName")}
txtPassword {$("#pwd")}
btnLogin (required:false) {$("#Login")}
}
def login(String userName, String pwd){
// ...
}
}
class LoginSpec extends GebSpec {
def "verify logic scenario" {
given: 'Username and Password'
def username = "abc"
def password = "jdaafafjadfgajffaghfg"
when: 'Fire Login URL'
to LoginPage
and: 'Enter Valid username,password and click on Login button'
login(username, password)
then: 'It should successfully and redirect to homepage'
at HomePage
}
// ...
}
Here it's not easy to navigate to login method with one click.
Do we have any plugin or setting to make easy this navigation?

You can place the cursor on the page class name and just press Ctrl-B or use Ctrl-LeftMouse in order to get there. As for easier navigation and code completion, I suggest assigning the result of to MyPage to a variable and then use that variable for calling page methods, e.g.
class LoginSpec extends GebSpec {
def "verify logic scenario" {
given: "user name and password"
def username = "abc"
def password = "jdaafafjadfgajffaghfg"
when: "opening login page"
def loginPage = to LoginPage
and: "logging it with valid credentials"
loginPage.login(username, password)
then: "after successful login the user gets redirected to the homepage"
at HomePage
}
}
Now Ctrl-B and Ctrl-LeftMouse should also work for your method calls and page element references such as loginPage.txtUsername.

Related

How to verify error messages using Geb

How do i capture the error messages generated on web page(for example an error message generated when a login button is pressed without inserting any username or password), how do i extract this error message "Username is required" using geb.
im using geb groovy based automation
So you would first model your page, and get the locator for the error you want to assert has been displayed. Note that you want to set this to required: false as you would not expect it to be displayed when you first land on the page.
Page with example page error with ID of #pageError:
class MyPage extends Page
{
static url = "/mypageurl"
static at = {
title == "My Page"
}
static content = {
pageError (required: false) { $('#pageError') }
submitButton { $('#mySubmitButton') }
}
def submitForm() {
submitButton.click()
}
}
Then you have your test:
def "If I try click Submit without filling out all form details show me an error message"()
{
when: "I click Submit without completing all form fields"
def myPage = to(MyPage)
myPage.submitForm()
then: "I am shown an error message on the same page"
myPage.pageError.displayed
}
EDIT:
Noticed you mentioned getting the content of the error, you would call this instead to get the text:
myPage.pageError.text()

MVC 4 website back press return the same form with old values

I am new in MVC 4 web development and i am creating a control panel. I have developed a Add user page and submit information in database successfully.
But after submit when i press back button it will show previous form.
i am using redirection the page to same page after submit form.
here is the code to redirect
public ActionResult AdminPanel(RegisterUserModel user)
{
if (ModelState.IsValid) // Check the model state for any validation errors
{
if (user.AddUserToDB(user.username, user.password, user.fullName,user.contactNo,user.COAId)) // Calls the Login class checkUser() for existence of the user in the database.
{
TempData["SuccessMessage"] = "User Added Sucessfully!";
ModelState.Clear();
return Redirect("AdminPanel");
}
else
{
ViewBag.SuccessMessage = "User Not Added";
return View();
}
}
SelectList clientsList = GetClinetList();
ViewBag.clientsList = clientsList;
return View(); // Return the same view with validation errors.
}
I have tried many examples but issue not resolved yet so kindly give my suggesstions
If you don't want the user to be able to see the previous content when clicking back, then you must indicate that content should not be cached by the browser and must revalidate with the origin server
A summary of this behaviour is here - http://blog.55minutes.com/2011/10/how-to-defeat-the-browser-back-button-cache/
You could create a nocache attribute, like this one - https://stackoverflow.com/a/10011896/1538039, and apply it to your controller methods.

Change body class when it has aurelia-app attached

Shortly I have two shells/roots: one for login page and one for the rest of the application and switch between them given the user is logged in or not.
My aurelia-app is attached to body element but I have distinct body classes for login and the rest of the application.
What is the best way to also switch body classes depending on the active shell?
In my opinion, the easiest way is changing the body class in the main.js file. For instance:
let rootComponent = '';
aurelia.start().then(a => {
if (!app.isLoggedIn()) { //<--- example of func to check if the user is logged in
document.body.className = "example-class-1";
rootComponent = 'login/login';
} else {
document.body.className = "example-class-2";
rootComponent = 'app/app';
}
a.setRoot(rootComponent, document.body);
});
Hope this helps!

Laravel route with variable in the url

I am trying to implement the password reset logic. The user gets the link to reset the password in the email. The url looks like
http://example.com/reset/resetcode
I have the route defined for it:
Route::get('reset/{resetcode}', function(){
return View::make('users.reset_password');
});
The form is rendered in the view to submit the email, new password etc. For the post of the form I have route defined as:
Route::post('reset/{resetcode}', array( 'as' => 'reset', 'uses' => 'UserController#passwordReset'));
I grab the resetcode from post route inside the passwordReset controller below
public function passwordReset($resetcode)
{
$validation = Validator::make(Input::all(), UserModel::$rulesPasswordReset);
if ($validation->passes())
{
try
{
// Find the user using the user email address
$user = Sentry::findUserByLogin(Input::get('email'));
// Check if the reset password code is valid
if ($user->checkResetPasswordCode($resetcode))
{
// Attempt to reset the user password
if ($user->attemptResetPassword($resetcode, 'new_password'))
{
// Password reset passed
}
else
{
// Password reset failed
}
}
else
{
// The provided password reset code is Invalid
}
}
catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
{
echo 'User was not found.';
}
}
else return Redirect::route('reset')->withInput()
->withErrors($validation)
->with('title', 'resetrequestfailure')
->with('message', 'Seems like you made some errors.');
}
The problem I am having is when I do Redirect::route after the validation fails. I am getting the resetcode from the route defined for post. When validation fails, the redirect route messes up and I cannot get the resetcode the second time. The supposed url of format
http://example.com/reset/8f1Z7wA4uVt7VemBpGSfaoI9mcjdEwtK8elCnQOb
becomes
http://bcnet.org/reset/%7Bcode%7D
It has to do with /{resetcode} part of the route and this is variable, so how can I get the correct resetcode even after the validation fails meaning that the url remains intact. Or how can I fix it to the appropriate Redirect::route after the validation failure.
You need to include the $resetcode on your return
else return Redirect::route('reset', $resetcode)->withInput()
->withErrors($validation)

MVC page to page redirection

I'm new to MVC (using 4, framework 4.0) and I understand the basics but this page redirect isn't working as I expect. The application is a login/authentication which if the user successfully logs in it redirects them to the target application. That part works just fine. However the user may forget his/her login credentials, so I have a series of pages that will prompt the user for a registered email address and decoded captcha value. If that information is validated then another page prompts for a series of (up to 3) pre-determined security question answers (in the case of a password forgotten). If the security challenge question is successfully answered the user is redirected to a password change page. At any point in the process the user may click a cancel button which should redirect back to the login page and clear any state variables tracking their progress through the recovery process. The problem is I keep getting stuck on pages that even after a RedirectToAction("SomeAction", "SomeController"); I still stay on the page? The URI even changes on the browser but the page asking for email address or security question stays active. I'm using an ajax $.get() to call various actions for submit and cancel.
view is defined like this:
#using (Html.BeginForm("RecoverUserCredentialsByModel", "Account", FormMethod.Get, new { id = "form1" }))
{
<!--... three input controls and a submit and cancel button-->
<p>
<button id="btnSubmit" onclick="return CheckUserEmail()">Submit</button>
<button id="btnCancel" onclick="return CancelRecovery()">Cancel</button>
</p>
}
<script type="text/javascript">
function CheckUserEmail() {
var emailAddress = document.getElementById("EmailAddress").value;
var pictogramHref = document.getElementById("pictogramHref").src;
var pictogramAnswer = document.getElementById("Pictogram").value;
if (emailAddress != null) {
var url = "/Account/ValidateEmail";
$.get(url, { "emailAddress": emailAddress, "pictogramHref": pictogramHref, "pictogramTranslation": pictogramAnswer }, null);
return true;
}
}
</script>
<script type="text/javascript">
function CancelRecovery() {
var url = "/AuthenticationModule/Account/CancelRecovery";
$.get(url, {}, null);
return true;
}
</script>
Codebehind redirections look like:
/// <summary>
/// Global cancel recovery, clears the stateful session object and redirects back to login view
/// </summary>
/// <returns>ActionResult</returns>
[AllowAnonymous]
public ActionResult CancelRecovery()
{
LoginModel statefulLoginModel = null;
try
{
// Reset everything active and redirect to login view
statefulLoginModel = new LoginModel();
Session["LoginModel"] = statefulLoginModel;
return Redirector(statefulLoginModel);
}
catch (Exception ex)
{
// Log the error and Reset everything active and redirect to login view
FileLogger.Log(ex);
statefulLoginModel = new LoginModel();
Session["LoginModel"] = statefulLoginModel;
return Redirector(statefulLoginModel);
}
}
[AllowAnonymous]
public ActionResult Redirector(LoginModel model)
{
... some code
Session["LoginModel"] = statefulLoginModel;
if (loginState == 0)
{
RedirectToAction("LogOn");
}
}
When it hits the RedirectToAction("LogOn"); the view "RecoverUserInfo" stays active on the browser and no redirection occurs?
What am I doing wrong?
Try this..........
Proper Syntax is return RedirectToAction("ActionName","ControllerName");
In this case if Logon Action is written on the same Controller Then use following Code..
return RedirectToAction("LogOn");
or it is written on another controller then just replace your Action Name and Controller Name in the following code.
return RedirectToAction("ActionName","ControllerName");