【发布时间】:2015-06-23 12:32:29
【问题描述】:
我正在尝试在 C# 上使用 Selenium 对我的一个网站进行自动化测试。该网站提示用户输入登录凭据。它只能通过多线程来完成。 我执行一个线程以在特殊的 FireFox 配置文件中打开页面,并并行执行另一个线程来模拟 ENTER 的按下。
问题是如果我不使用多线程,代码在打开页面时停止,因为网站没有完全打开。 驱动程序在创建的第一个线程中启动。但是它必须在 main 方法中使用。 如何在主函数中传递在方法(由线程执行)中创建的对象?
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using System.Collections.ObjectModel;
using System.Threading;
using System.IO;
using OpenQA.Selenium.Interactions;
using System.Windows.Forms;
namespace CRMTest1WithSelenium
{
class Program
{
public static IWebDriver OpenPage()
{
IWebDriver driver = null;
string path = @"C:\Users\ntouma\AppData\Roaming\Mozilla\Firefox\Profiles\nuxgc36b.CRMTester";
FirefoxProfile ffprofile = new FirefoxProfile(path);
driver = new FirefoxDriver(ffprofile);
driver.Navigate().GoToUrl("http://www.example.com");
return driver;
}
public static void pressEnter()
{
Thread.Sleep(6000);
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
}
static void Main(string[] args)
{
IWebDriver driver2 = null;
driver2 = OpenPage();
// Thread driverCreation = new Thread(OpenPage);
// driverCreation.Start();
Thread login = new Thread(pressEnter);
login.Start();
Thread.Sleep(2000);
driver2.Manage().Window.Maximize();
IWebElement sales = driver2.FindElement(By.XPath("/html/body/div[4]/d"));
sales.Click();
IWebElement leads = driver2.FindElement(By.XPath("/html/body//div/div/ul/li[3]/span/span[2]/span/span[1]/span/a[1]"));
leads.Click();
Thread.Sleep(5000);
}
}
}
【问题讨论】:
标签: c# multithreading object selenium parameter-passing