1

I would like to hear your opinion on how I am organizing our large amount of Elements found in our web application.

Currently we are using selenium to run functional test cases. Our test cases reach into the thousands.

My issue is, the current method I use to store our objects on the site is through JSON. I parse the object into a dynamic object, then I take that dynamic object and force it into a custom class.

for example if we had a login page with username, password and a login button it might look something like this

dynamic page = jsonObj.LoginPage
page.Username.ToObject<ElementObject>().OnSendKeys("Username");
page.Password.ToObject<ElementObject>().OnSendKeys("Password");
page.LoginBtn.ToObject<ElementObject>().OnClick();

The ElementObject then finds out how to access the object and then clicks it, sends keys. what ever.

Can anyone forsee whats wrong with this? I feel like this can be done in a much better way.

{
  "Login": {
    "UserName": {
      "Id": "Username_Input"
    },
    "Password": {
      "Id": "Password_Input"
    },
    "LoginButton": {
      "Id": "Login_BTN"
    }
  }
}

ElementObject Class:

public class ElementObject
    {
        public string ClassName;
        public string CssSelector;
        public string Id;
        public string LinkText;
        public string Name;
        public string PartialLinkText;
        public string TagName;
        public string XPath;

        public ElementObject();

        public int GetIndex();
        public string GetText();
        public string GetValue();
        public void OnClick();
        public void OnHover();
        public void OnSelectByIndex(int pValue);
        public void OnSelectByText(string pValue);
        public void OnSelectByValue(string pValue);
        public void OnSendKeys(string pMsg);
    }

1 Answer 1

1

Typically i find the best way to organize elements in a selenium based test is to use the page object pattern: https://martinfowler.com/bliki/PageObject.html (there is a bunch of literature out there this is just one example)

The way it works is you define a class for each page of your application and fill that class with all of the elements and functions of that page for example:

I don't know the syntax 100% so I am just going to use sudo code in my example

class LoginPage{
    // username box element
    // password box element
    // signIn Button element

    public void login(String username, String password){
        //in here you would interacte with the above elements to hopefully log in
    }

then from your test script you simply need to call LoginPage.login("myUsername", "MyPassword")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.