简体   繁体   中英

How to convert the datatable into list of webelement in C#

I am new to C# and I am using specflow

I am automating a scenario where multiple element needs to be checked if they are enabled. These elements are from different section of the page and have different xpath.

Approach I am thinking is

  1. Convert the datatable to list of webelements

  2. Iterate over the list with

    foreach (webelement e in list) { element.isEnabled(); }

I am planning to keep the element name same as the name given in datatable.

Have edited my question

Edit

My scenario

Scenario: Validate G Functionality
    Given Open URL
    When verifies fields
        | Gmail  |
        | Images |

Step Def

[When(@"verifies fields")]
        public void WhenVerifiesFields(Table table)

        {
           for(int i=0;i<=table.Rows.Count;i++)
            {


                IWebElement ew= table.Rows[i] as IWebElement;
                Assert.False(ew.Enabled);
            }

Page class:

[FindsBy(How = How.LinkText, Using = "Gmail")]
    private IWebElement Gmail;

    [FindsBy(How = How.LinkText, Using = "Images")]
    private IWebElement Images;

I am getting following error

    Validate G Functionality [FAIL]
    [xUnit.net 00:00:06.38]       System.NullReferenceException : Object reference not set to an instance of an object.
    [xUnit.net 00:00:06.38]       Stack Trace:
    [xUnit.net 00:00:06.38]         /*line pointed here is 
 ew.enabled*/C:\Users\StepDefinition\TestStepsOne.cs(53,0): at BillTrackerAutomation.StepDefinition.TestStepsOne.WhenVerifiesFields(Table table)
    [xUnit.net 00:00:06.38]            at lambda_method(Closure , IContextManager , Table )

There are a number of ways to convert datatable to a list of elements, here is a linq based example...

List<WebElement> studentList = new List<WebElement>();  
studentList = (from DataRow dr in dt.Rows  
        select new WebElment()  
        {  
            StudentId = Convert .ToInt32 (dr["StudentId"]),  
            StudentName = dr["StudentName"].ToString(),  
            Address = dr["Address"].ToString(),  
            MobileNo = dr["MobileNo"].ToString()  
        }).ToList();  

Adding a method to your page model would allow you to retain encapsulation of your web elements, but also provide a parameterized way to test the elements:

public bool IsFieldEnabled(string fieldName)
{
    switch(fieldName)
    {
        case "Gmail":
            return Gmail.Enabled;
        ...
    }
}

The step:

When verifies fields
    | Field  |
    | Gmail  |
    | Images |

The step definition:

[When(@"verifies fields")]
public void WhenVerifiesFields(Table table)
{
    foreach (var row in table.Rows)
    {
       var isEnabled =  yourPageModel.IsFieldEnabled(row["Field"]);

       Assert.False(isEnabled);
    }
}

You can achieve that with [StepArgumentTransformation]

  Scenario Outline: examples with step argument
  Given we have '<Webelements>'

  Examples:
  | Webelements | 
  |     Gmail   |

And step definitions are:

  using TechTalk.SpecFlow;
  using OpenQA.Selenium.Chrome;
  using OpenQA.Selenium.Support.UI;
  using NUnit.Framework;

[Binding]
   public class Browser
  {
     private readonly BrowserDriver _browserDriver;
     public static  OpenQA.Selenium.IWebDriver driver ;

      public Browser(BrowserDriver browserDriver, FeatureContext featureContext)
    {
        _browserDriver = browserDriver;  //your chrome driver
        driver = _browserDriver._driver;
        _featureContext = featureContext;
        _featureContext.Add("driver", driver);
    }

     [Given(@"we have '(.*)'")] 

    public void webelements(ChromeWebElement o)
    {
       Assert.False(o.Displayed);
    }

    [StepArgumentTransformation]
      public ChromeWebElement convertToWebElement(string c)
      {
        ChromeDriver parent = _featureContext.Get<ChromeDriver>("driver");
        return new ChromeWebElement(parent, c);
    }  

And obviously you get result element is not attached to the page document because I just used a Gmail for test purpose

But it is converting string to ChromeWebElement class (thats why you get that error).

Note that you can not convert to IWebElement directly because it is an interface

Also, note that you can not use [StepArgumentTransformation] with specFlow tables. It is mentioned in Restrictions part in here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM