Jawad
Jawad

Reputation: 1

Selenium WebDriver Generic Test

I am exploring selenium web driver in c#. I want to make my test as generic as possible.What i want is that when my form loads all the text fields are identified and filled in with some dummy data. When I use the switch statement as specified below only first text field is filled up. How can I make it get all the fields and fill them one by one

foreach (var group in form.GroupFormFields)
{
   foreach (var field in group.FormFields)
   {
      var fieldId = "txt_" + field.field_title;

      switch (field.field_type_id)
      {
         case "textfield":
            var textfield = Driver.Instance.FindElement(By.XPath("//input[@type='text']"));
            textfield.SendKeys("abc");
            //field.field_title
            //fill the form field
            break;
         case "email":
            var email = Driver.Instance.FindElement(By.XPath("//input[@type='email']"));
            email.SendKeys("[email protected]");
            break;
         case "password":
            var password = Driver.Instance.FindElement(By.XPath("//input[@type='password']"));
            password.SendKeys("Abc1234%");
            break;
         case "checkbox":
            var checkbox = Driver.Instance.FindElement(By.XPath("//input[@type='checkbox']"));
            checkbox.Click();
            break;
            default:
            break;
      }
   }
}   

Upvotes: 0

Views: 1056

Answers (3)

OriBr
OriBr

Reputation: 324

Develop automation project using selenium can be hard to maintain since sometimes we required to automate big and complicated systems, best practice is to use Page Object design pattern. Implementing Page Object in an automation project will help the coder to stick to the SOLID principles. You can wrap your page object with a class that in charge on the business logic such - fill the form. Please review the example below

enter image description here

Wrap it and implement the fill details section

enter image description here

Now all you have to do is to call pageObjectContainer.FillDetails(submit: true)

Upvotes: 1

Tony Bui
Tony Bui

Reputation: 815

I think you can create a List with:

  • Key: id of the input
  • Value: value you want to input

the create a for loop looking through the List, findind the Element by Key and input with Value.

Upvotes: 0

Gaurang Shah
Gaurang Shah

Reputation: 12930

Switch case is to execute particular code on particular condition, so based on what you are passing as caseField as particular case will be invoked Not All.

If you want to fill all the input fields with dummy data, the simple thing is to get all the input elements and iterate though them. I don't C#, however JAVA and C# is very similar so perhpehs you could understand this code.

List<WebElement> inputs = driver.findElements(By.xpath("//input"));
for(WebElement input: inputs){
    input.sendKeys("filled through automation");
}

Upvotes: 1

Related Questions