我怎么能避免引用Excel表每次我需要一个数据块

本文关键字:数据 一个 能避免 引用 Excel | 更新日期: 2023-09-27 18:12:59

我一直致力于构建一个Selenium框架来测试我工作的web应用程序。我最近一直在尝试数据驱动测试,使用外部Excel表格来存储CSS元素名称,以及提交表单等的数据。我在下面包含了我的代码,正如你所看到的,我正在"调用"Excel来获取我需要的每一点数据。这要花很长时间。是否有一种方法可以一次性导入所有数据并"在内存中"进行操作?

public string getCell(string sheet, string a)
    {
        string workbookPath = @"C:'exceltest.xlsx";
        Excel.Application excelApp;
        Excel.Workbook excelWorkbook;
        Excel.Sheets excelSheets;
        excelApp = new Excel.Application();
        excelWorkbook = excelApp.Workbooks.Add(workbookPath);
        excelSheets = excelWorkbook.Sheets;
        excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(sheet);
        Excel.Range range = excelWorksheet.get_Range(a, a);
        string cell = range.Value.ToString();
        return cell;
    }
    [Test]
    public void addGuest()
    {
        newSession();
        driver.FindElement(By.CssSelector(getCell(css, "B11"))).Click();
        driver.FindElement(By.CssSelector(getCell(css, "B12"))).Click();
        driver.SwitchTo().Window(driver.WindowHandles.Last());
        driver.FindElement(By.CssSelector(getCell(css, "B13"))).SendKeys(getCell(property, "B3"));
        driver.FindElement(By.CssSelector(getCell(css, "B14"))).SendKeys(getCell(property, "B4"));
        driver.FindElement(By.CssSelector(getCell(css, "B15"))).SendKeys(getCell(property, "B5"));
        driver.FindElement(By.CssSelector(getCell(css, "B16"))).SendKeys(getCell(property, "B6"));
        driver.FindElement(By.CssSelector(getCell(css, "B17"))).SendKeys(getCell(property, "B7"));
        driver.FindElement(By.CssSelector(getCell(css, "B18"))).SendKeys(getCell(property, "B8"));
        driver.FindElement(By.CssSelector(getCell(css, "B19"))).SendKeys(getCell(property, "B9"));
        driver.FindElement(By.CssSelector(getCell(css, "B20"))).SendKeys(getCell(property, "B10"));
        driver.FindElement(By.CssSelector(getCell(css, "B21"))).SendKeys(getCell(property, "B11"));
        driver.FindElement(By.CssSelector(getCell(css, "B22"))).SendKeys(getCell(property, "B12"));
        driver.FindElement(By.CssSelector(getCell(css, "B23"))).SendKeys(Keys.Down);
        driver.FindElement(By.CssSelector(getCell(css, "B24"))).SendKeys(Keys.Down);
        driver.FindElement(By.CssSelector(getCell(css, "B25"))).SendKeys(getCell(property, "B13"));
        driver.FindElement(By.CssSelector(getCell(css, "B26"))).SendKeys(getCell(property, "B14"));
        driver.FindElement(By.CssSelector(getCell(css, "B27"))).Click();
    }
    [Test]
    public void newSession()
    {
        string baseURL = "https://website.com";
        string property = "Property";
        //Login
        driver.Navigate().GoToUrl(baseURL);
        driver.FindElement(By.CssSelector(getCell(css, "B1"))).SendKeys(getCell("Sheet1", "A1"));
        driver.FindElement(By.CssSelector(getCell(css, "B2"))).SendKeys(getCell("Sheet1", "B1"));
        driver.FindElement(By.CssSelector(getCell(css, "B3"))).Click();
        //Property>New Session
        driver.FindElement(By.CssSelector(getCell(css, "B4"))).SendKeys(getCell(property, "B1"));
        driver.FindElement(By.CssSelector(getCell(css, "B5"))).Click();
        driver.FindElement(By.CssSelector(getCell(css, "B6"))).Click();
        driver.FindElement(By.CssSelector(getCell(css, "B7"))).Click();
    }

我怎么能避免引用Excel表每次我需要一个数据块

你真的不想这样使用Excel。你最好把CSS选择器放在代码中。Excel更适合用于数据验证之类的事情。例如,我在电子商务网站工作(想想亚马逊网站或任何卖东西的网站)。我有Excel表格中的产品数据…诸如产品id、名称、数量、定价等。我有一个特定格式的表格,我有一个脚本,每次读取一行,并通过网站和购买产品,同时验证不同的数据位。每周我都会收到一两张新纸……这是一个更好的使用Excel,在我看来。

我会这样做…

public class Test
{
    private By sampleElementLocator = By.cssSelector("sampleId");
    public void addGuest()
    {
        driver.findElement(sampleElementLocator).click();
        ...
    }
}