Problem with PageFactory.InitElements in WebDriver API c# .NET - webdriver

I am trying to create WebDriver UI tests framework using Page Object pattern, using the following URL as a reference: http://www.peternewhook.com/2010/09/automated-testing-pageobjects-webdriver/
As per example I have created 3 classes (see below). The problem is with the line return PageFactory.InitElements(_driver, page); in the Search method of the SearchPage class.
When I try to build I get the following error:
The type 'OpenQA.Selenium.ISearchContext' is defined in an assembly that is not referenced. You must add a reference to assembly 'WebDriver
Fair enough, as I am referencing WebDriver.Common.dll, so I tried removing it and added WebDriver.dll to my References and all of a sudden I get the following when I build:
Cannot implicitly convert type 'void' to 'ConsoleApplication1.ResultsPage'
and it fails on the same line; when I hover over it, it says:
Cannot convert expression type 'void' to 'ConsoleApplication1.ResultsPage'.
I also tried referencing both assemblies and thought I could use different usings but it is a no-go, didn't work.
Why can't PageFactory.InitElements be returned when using WebDriver.dll?
Is there a way around it, or can I achieve the same result by changing the architecture slightly?
Your help is much appreciated. Thanks.
using OpenQA.Selenium;
namespace ConsoleApplication1
{
public class Page
{
public IWebDriver _driver;
public Page(IWebDriver driver)
{
this._driver = driver;
}
}
}
using OpenQA.Selenium;
namespace ConsoleApplication1
{
public class ResultsPage : Page
{
public ResultsPage(IWebDriver driver)
: base(driver)
{
}
private IWebElement count;
public string GetPagesReturned()
{
return count.Text;
}
}
}
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
namespace ConsoleApplication1
{
public class SearchPage : Page
{
public SearchPage(IWebDriver driver) : base(driver)
{
}
private IWebElement q;
private IWebElement go;
public ResultsPage Search(string searchStatement)
{
q.SendKeys(searchStatement);
go.Click();
ResultsPage page = new ResultsPage(_driver);
return PageFactory.InitElements(_driver, page);
}
}
}

The problem is that PageFactory.InitElements() returns void. Rather, it modifies the page you've passed in. Your code should look something like this:
public ResultsPage Search(string searchStatement)
{
q.SendKeys(searchStatement);
go.Click();
ResultsPage page = new ResultsPage(_driver);
PageFactory.InitElements(_driver, page);
return page;
}

Looks like C# PageFactory does not init private superclass elements (via PageFactory.InitElements) per this:
http://code.google.com/p/selenium/issues/detail?id=1189#makechanges

Using PageFactory.InitElements(_driver, this); on the constructor of your base page class .
public class Page
{
public IWebDriver _driver;
public Page(IWebDriver driver)
{
this._driver = driver;
PageFactory.InitElements(_driver, this);
}
}

I agree with JimEvans. But instead of having PAgeFactory code in seperate place, have that in constructor of Results page.
Your results page class should be something like this.
public class result
{
public result(driver,this)
{
}
}

Related

any work around to allow for an action result to accept a Abstract class as parameter

I have different configurations all inheriting from a base configuration that are customized in forms. I want all of these to be handled by a single action result.
[HttpPost]
public IActionResult Register(AbstractBaseConfig config)
{
...do some logic...
return View("../Home/Index");
}
However, this is not possible because you cannot base in abstract classes as a parameter to an action result. Is there any work around for this so I don't need a seperate action result for each configuration? (I still want each configuration to be it's own class, I only need access to the base class methods in the action result logic).
Basically you can't, and the reason is that MVC will try to do new AbstractBaseConfig() as part of the Data Binding process (which parses the URL or the Form Post and puts the results in a concrete object). And by definition, doing new AbstractBaseConfig() is impossible for an abstract class.
It also makes sense for other reasons, I will explain why.
You seem to expect that MVC can determine the class from the parameters that are being passed in. That is not how it works, in fact the opposite is true: the Action Method has to specify the exact class, and then the Binder will instantiate that exact class and try to bind its properties.
Suppose you had this:
public abstract class Thing { public int ID { get;set; } }
public class NamedThing : Thing { public string Name { get;set; } }
public class OtherThing : Thing { public string Name { get;set; } }
and suppose it would be allowed to use:
public IActionResult Register(Thing thing)
then what would you expect to be in thing after Data Binding: a Thing object with only the ID set? Or one of the other object types, with Name set, but how would MVC ever be able to know which class you meant?
So for all these reasons, this is not possible.
You could have a base class inherit the abstract class and all your classes inherit from that base class whilst having that base class as your parameter
Take for example
public abstract class ABase
{
public void stuff()
{
var stuff = string.Empty;
stuff = "hello";
}
public virtual void otherstuff()
{
var stuff = string.Empty;
stuff = "hello";
}
}
public class Base : ABase
{
//empty
}
public class Derived : Base
{
public void mystuff()
{
this.stuff();
}
public override void otherstuff()
{
// Custom code
}
}
public ActionResult Register(Base config)
{
}

Change default session provider in ASP.NET

I want to change my session proviced to statically typed - I just hate typing strings because of many many errors I do.
What technology am I using? ASP.NET MVC via EXT.NET MVC
I was trying to do that using web.config but the problem is that after add session state to it visual is not going to compile my code because of that session should be using strings as keys.
I want to use session by enums such as :
public enum SessionEnum{Model}
public class Bar{
void foo(){
Session[SessionEnum.Model] = "blah";
}
}
I am aware that I can create wrapper converting enums to strings but it's not very satisfying solution for me.
public class StorageWrapper{
public object this[SessionEnum enum]{ get{return Session[enum.toString()]}; //+set
}
What I did was create static object for base class for all of my controllers and then I was able to use it across them but after closing and opening the page again I wasn't able to get values from it. I guess I should serialize them somehow but I have no idea how.
Is there any way to do that?
EDIT
My session now looks like this :
[Serializable]
public abstract class DataWrapper<T> : HttpSessionStateBase
{
Dictionary<T, object> Dictionary { get; set; } = new Dictionary<T, object>();
public object this[T a]
{
get
{
try
{
return Dictionary[a];
}
catch
{
return null;
}
}
set { Dictionary[a] = value; }
}
}
[Serializable]
public class SessionWrapper : DataWrapper<SessionNames>
{}
public enum SessionNames { Model, Login, LastOpenedFile }
It's very simple.
Create a UserSession object which does everything you want (holds your values as enum etc), instantiate it, then put it in the session.
var US = new UserSession();
US.stuff = somestuff;
Session["UserSess"] = US
Then you can just always use Session["UserSess"].stuff;
Mmmm, wouldn't you use static const string instead of an enum?
using System.Web;
public static class SessionEnum
{
public static const string Model = "_Session_Model";
public static const string Login = "_Session_Login";
public static const string LastOpenedFile = "_Session_LastOpenedFile ";
}
class test
{
void test()
{
Session[SessionEnum.Model] = "blah";
}
}

MVC 6 #inherit RazorPage

I am trying to migrate an MVC 5 Application to ASP.NET 5 MVC 6 (Beta 7).
Having problems when using the #inherits and #model directive together.
Works fine when they are used separately.
In my _ViewImports i added the #inherits directive to use a base page with some custom user properties.
public abstract class BaseViewPage<TModel> : RazorPage<TModel>
{
protected MyPrincipal AppUser
{
get
{
return new MyPrincipal(this.User as ClaimsPrincipal);
}
}
}
_ViewImports.cshttml
#inherits CommonWeb.BaseViewPage<TModel>
#addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"
And then i can go AppUser. in all my views.
This works if i dont use a strongly typed view. If i add the #model directive in any view the inherited view page goes away.
Help appreciated
Update:
I did this successfully by using a custom pageBaseType in the web.config in prior versions.
Workaround.
public class ViewHelper
{
ViewContext _context;
public ViewHelper(ViewContext context)
{
_context = context;
}
public MyPrincipal AppUser
{
get
{
return new MyPrincipal(_context.HttpContext.User as ClaimsPrincipal);
}
}
public string ControllerName
{
get
{
return _context.RouteData.Values["controller"].ToString();
}
}
}
View:
#{ var viewHelper = new ViewHelper(ViewContext);}
A way to achieve this for all views?
There is a better way in MVC 6, which now supports injecting dependencies on the views with the #inject directive. (The directive #inject IFoo Foo allows you to use in your view a property named Foo of type IFoo)
Create a new interface IAppUserAccessor for getting your app user, for example:
public interface IAppUserAccessor
{
MyPrincipal GetAppUser();
}
Create a class AppUserAccessor implementing it:
public class AppUserAccessor : IAppUserAccessor
{
private IHttpContextAccessor httpContextProvider;
public AppUserAccessor(IHttpContextAccessor httpContextProvider)
{
this.httpContextProvider = httpContextProvider;
}
public MyPrincipal GetAppUser()
{
return new MyPrincipal (
httpContextProvider.HttpContext.User as ClaimsPrincipal);
}
}
Register the new interface in the services container by adding a new entry in the ConfigureServices method of Startup.cs:
services.AddTransient<IAppUserAccessor, AppUserAccessor>();
Finally use the #inject directive to inject the IAppUserAccessor in your views. If you add the directive in ViewImports.cshtml then it will be available on every view.
#inject WebApplication4.Services.IAppUserAccessor AppUserAccessor
With all the pieces above you can now just use it on your view(s):
#AppUserAccessor.GetAppUser()
Update
If you need to inspect the route values, like the controller name, you can inject an IActionContextAccessor into your class and use it as follows:
public AppUserAccessor(IHttpContextAccessor httpContextProvider, IActionContextAccessor actionContextAccessor)
{
this.httpContextProvider = httpContextProvider;
this.actionContextAccessor = actionContextAccessor;
}
...
public string ControllerName
{
get { return actionContextAccessor.ActionContext.RouteData.Values["controller"].ToString(); }
}
Of course, that doesn't look like an AppUserAccessor anymore and smells like it has different responsabilities. At the very least it needs a more appropriate name :)
I would double check what do I need the controller name for. There might be a better way to accomplish your objective. (For example, if you need it for generating new links/urls you might just use an IUrlHelper)
Accessing ViewContext
Looks like beta8 has added support for injecting the ViewContext, although the implementation details may change before RC. See this question

Call Public Property Declared in a ASPX Page from a Different ASPX Page

How can I call a public property declared on a ASPX page from a different ASPX Page? Is that possible? It is a website project. How can I get/call this property from a different aspx page? I have attempted this from the other page, but it is not recognizing the partial class: private Test_Default _test; It does not recognize the "Test_Default"
I.E.
public partial class Test_Default : System.Web.UI.Page
{
private string myAge = string.empty;
public string Name
{
get
{
return myName;
}
set
{
myName = value;
}
}
If you need a piece of shared code, create a class for it in the APP_CODE folder.
See MSDN documentation here and here.
I would create a separate class exposing the property you are wanting to work with, and store the value in Session :
public class MyClass
{
public static string MyName
{
get
{
if (Session["MY_NAME"] != null)
{
return Session["MY_NAME"].ToString();
}
return String.Empty;
}
set { Session["MY_NAME"] = value; }
}
}
You should be able to call that from either Page. If it's a complex object, then change the type from string to your object.
Hope that helps!!

sharp architecture question

I am trying to get my head around the sharp architecture and follow the tutorial. I am using this code:
using Bla.Core;
using System.Collections.Generic;
using Bla.Core.DataInterfaces;
using System.Web.Mvc;
using SharpArch.Core;
using SharpArch.Web;
using Bla.Web;
namespace Bla.Web.Controllers
{
public class UsersController
{
public UsersController(IUserRepository userRepository)
{
Check.Require(userRepository != null,"userRepository may not be null");
this.userRepository = userRepository;
}
public ActionResult ListStaffMembersMatching(string filter) {
List<User> matchingUsers = userRepository.FindAllMatching(filter);
return View("ListUsersMatchingFilter", matchingUsers);
}
private readonly IUserRepository userRepository;
}
}
I get this error:
The name 'View' does not exist in the current context
I have used all the correct using statements and referenced the assemblies as far as I can see. The views live in Bla.Web in this architecture.
Can anyone see the problem?
Thanks.
Christian
You should inherit UsersController from System.Web.Mvc.Controller class. View() method is defined in Controller class.
public class UsersController : Controller
{
//...
}

Resources