I've created a cucumber feature file as follows
Feature: Managing Students
Scenario: Manage Students page
Given Open browser and launch application
And Home page is displayed
When User clicks on Manage Students link
Then User sees Manage Students page
Scenario: Create Students Page
Given Click on Add student button
And New form is displayed
Then Enter details in the form
Then Click submit
And a Java file as follows
#SneakyThrows
#When("^User clicks on Manage Students link$")
public void clickOnManagerStudentsLink(){
String manageStudentsBtnText = webDriver.findElement(By.id("manage-students-btn")).getText();
Assert.assertTrue(manageStudentsBtnText.equalsIgnoreCase("Manage Students"));
webDriver.findElement(By.id("manage-students-btn")).click();
}
#SneakyThrows
#Then("^User sees Manage Students page$")
public void userSeesManageStudentsPage(){
String manageStudentsBtnText = webDriver.findElement(By.id("add-student-btn")).getText();
Assert.assertTrue(manageStudentsBtnText.equalsIgnoreCase("Add Student"));
}
#SneakyThrows
#Given("^Click on Add student button$")
public void clickOnAddStudentButton(){
webDriver.findElement(By.id("add-student-btn")).click();
Assert.assertTrue(webDriver.findElement(By.xpath("//h2[contains(text(),'Add Student')]")).getText().equalsIgnoreCase("Add Student"));
}
Now, when I run a test file, the first scenario executes perfectly, but when it comes to first line of clickOnAddStudentButton() it throws NullPointerException. TIA.
Related
Using spring boot, I have a timetable page.
You can click a square, which opens up a small form with which you can add or remove the class objects from the timetable (which are saved in a database).
My issue is, when you click 'add' (or remove), while it successfully adds/removes that object from the timetable, you have to refresh the page in order to see the class be added/removed on the timetable. It appears like nothing happens when add is clicked from the user's perspective (until they manually refresh the page).
The post method redirects back to the timetable page get. I tried having it redirect to another mini get method, which then redirected back to the original timetable page; but from the browser side it still didn't look like anything was happening - just remaining on the same page with the original problem. Would love to hear a potential solution, thanks!
Edit: Here's an example of my get and post methods:
#GetMapping("/timetable/{id}/{semId}")//This method displays timetable.
public String timetable(#PathVariable(value = "id") String id, Model model,
#PathVariable(value = "semId") String semId) {
//code..
model.addAttribute("x", x);
return "timetable";
}
#PostMapping("/timetable/{id}/{semId}")
public String timetablePost(#ModelAttribute TimetableClassDto dto, #PathVariable(value = "id") String id,
Model model, #PathVariable(value = "semId") String semId) {
//code..
return "redirect://timetable/00/" + semId;
}
Are you supposed to have two // in your redirect? I have something similar in my code and it works fine. However, I create a url first then return that. Also, make sure your get mapping is properly filling out the object based on the new parameters gotten from the redirect.
Use the following:
??
String url = "redirect://timetable/00/" + semId;
return url;
I have weird problem that I cannot solve. I have an ASP.NET MVC Core 2 application using EF Core. I have a button that triggers an action method. I click the button, it redirects me to the view where I click another button, that makes some database operations and brings me back to the original page.
And inside that method I simply use my ApplicationDbContext:
MyEntry myEntry = _db.MyEntries.FirstOrDefault(e => e.MyProperty == myValue);
The thing is that when I go that path several times, it works. And it stops working on the eighth call, it's always the same line (executing database) that suddenly starts taking about half a minute.
When I modify my code (e.g. move some database code outside loop, create collection and then use it inside loop) it stops working at the other moment - always the same line of code, after the same number of executions, but in a different place.
Any ideas what could be wrong?
I simply use the database in my controller:
private readonly ApplicationDbContext _database;
public MyController(ApplicationDbContext database)
{
_database = database;
}
And ApplicationDbContext is:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
Thanks!
It feels like the MyEntry entity needs a key:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<MyEntry>()
.HasKey(b => new { b.Id, b.MyProperty});
}
I have a model class like following
public class ProductModel
{
string ProductName { get; set; }
int Quantity { get; set; }
}
In Controller I have an Action item
public ActionResult ShowProduct()
{
return View();
}
In my view user has two text boxes; where they enter product name and quantity. The first time they come in on this page these fields are empty. Once they enter values in these text boxes they hit a Next button which take them to a next page where they have to enter additional information about order.
On that page I have a back button and they can come back to this first page. Problem is I need to display the information that they entered in first page but on the second page I don’t have that ProductModel anymore. I can store that model in session but not sure if there is any better pattern of doing it in MVC
I would steer clear of Session and TempData. If you're using MVC, and your views are separated by full postbacks, (not Ajax) maybe you could use a view model pattern across different controller actions.
public class OrderController : Controller
{
public ActionResult ShowProduct()
{
return View(new ProductViewModel());
}
[HttpPost]
public ActionResult DoOrderStuff(ProductViewModel vm)
{
if (ModelState.IsValid)
{
// OrderViewModel would contain some product data
// to be used in the DoOrderStuff view
return View(new OrderViewModel(vm));
}
// error, go back to Page 1
return View("ShowProduct", vm);
}
}
This gives you room for validation while still following the wizard style views you described.
Caveat I just realized with this:
If you have a bunch of successive views, your user experience would probably suffer without a lot of hacking together of different view models. E.g. customer is on page 5 of the wizard, and wants to go back to page 2--my answer in its simplest form wouldn't accommodate that. However, with a good abstraction of the values in all your screens, it could be done.
This is pretty much what the Session dictionary was intended to be used for. You may look into using TempData but in essence it is just a lightweight version of Session. I don't see anything wroth with what you are doing.
I don't think you need to store this in the Session/TempData (watch out, how TempData works surprisingly changed quite a bit from MVC 2 to MVC 3). Your next button sounds like a POST, and then you do some sort of Redirect. If instead you made your form POST to the URL you wanted to display next, the ProductModel would be passed right along, and you could then pass it from the Action to the View, through either the Model or ViewData.
In my MVC application I am using membership service . I need a page to list the users. But there are 1000's of users are in my application. So i don't need to display all of them in one page.
I am planning to give a search option .I mean admin user can search by specifying user role and how many users can show in one page.How can i do this ? Any ideas?
current code
Model
public MembershipUserCollection Users { get; set; }
Controller
model.Users = Membership.GetAllUsers();
But i am getting all users in the application.
You probably want to query your role provider:
public ActionResult Foo()
{
string[] usernamesInRole = Roles.GetUsersInRole("some_role");
...
}
I have Person controler with List Edit Create methods after edit of Person i go back to Person list
NOW I have other controler Family (Family - Person is one-to-many )
on Detail Family I have list of Persons with edit linked to Person/Edit/Id
When user clicks Save Person after editing I want to go back to Family Detail if he came from there or to Peron list if he Edited Person from Person list
Some help please
Thanks
You can use
HttpContext.Request.UrlReferrer
So to go back to the last view you would use something like:
Response.Redirect(HttpContext.Request.UrlReferrer.OriginalString);
Edit after comment:
Indeed you would get send back to the edit page in this case, what you could do if you don't want anyone to see querystrings is: (assuming you are using html.form to post the data).
On your Edit person page, inside of the form add something like:
#using (Html.BeginForm(new { RedirectUrl = HttpContext.Current.Request.UrlReferrer.OriginalString } )
and then on the controller pick it up as:
public Void Edit(int id, string RedirectUrl)
{
//save stuff
return Redirect(RedirectUrl);
}
You can pass the URL of the previous page in the query string for the edit page.