2010-07-28 81 views
3

我试过谷歌,但有很多不同的方式来使用硒。我使用的是: - Windows 2003 Server的 - Visual Studio 2008中 - 通过Firefox浏览正常 安装硒IDE - NUnit的2.5被复制到C:\ - 硒RC被复制到C:\如何设置Selenium使用C#使用Visual Studio .NET?

  1. 首先我使用C#创建了一个库项目。
  2. 这我的课:
 
using System; 
using System.Text; 
using System.Text.RegularExpressions; 
using System.Threading; 
using NUnit.Framework; 
using Selenium; 

namespace SeleniumTest 
{ 
    [TestFixture] 
    public class NewTest 
    { 
     private ISelenium selenium; 
     private StringBuilder verificationErrors; 

     [SetUp] 
     public void SetupTest() 
     { 
      selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://localhost:4444"); 
      selenium.Start(); 
      verificationErrors = new StringBuilder(); 
     } 

     [TearDown] 
     public void TeardownTest() 
     { 
      try 
      { 
       selenium.Stop(); 
      } 
      catch(Exception) 
      { 
       // Ignore errors if unable to close the browser 
      } 
      Assert.AreEqual("", ""); 
     } 

     [Test] 
     public void TheNewTest() 
     { 
      selenium.Open("/google.com"); 
     } 
    } 
} 
  1. 下从C添加的所有引用:\硒RC \硒DOTNET客户端驱动器-1.0.1
  2. 编译图书馆计划,成功了。没有错误。
  3. 运行NUnit.exe,现在的错误:(

SeleniumTest.NewTest.TheNewTest: Selenium.SeleniumException : XHR ERROR: URL = http://localhost:4444/google.com Response_Code = 403 Error_Message = Forbidden+for+Proxy

回答

2

,因为你的设置基本URL以硒RC的你得到禁止错误。你需要将其设置为http://www.google.com,然后在你的测试会是什么样子

[Test] 
    public void TheNewTest() 
    { 
     selenium.Open("/"); 
    } 

,或者您需要您的测试改为

[Test] 
    public void TheNewTest() 
    { 
     selenium.Open("http://www.google.com"); 
    } 
+0

您好, 我在将它更改为

 [Test] public void TheNewTest() { selenium.Open("http://www.google.com"); } 
SeleniumTest.NewTest.TheNeTest: Sel enium.SeleniumException:错误:命令执行失败。请在http://clearspace.openqa.org上搜索论坛,查看日志窗口中的错误详情。错误消息是:访问被拒绝。 – Chan 2010-07-28 17:27:04

+0

将baseURL设置为http://www.google.com,然后打开(“/”); – AutomatedTester 2010-07-28 17:58:55

+0

我尝试了很多不同的方法。即使我创建了一个控制台应用程序而不是类库来测试它。但它似乎冻结而不是运行http://google.com。出现了Selenium Remote Control v.2。这是用Firefox打开的链接:http:// localhost:4444/selenium-server/core/Blank.html?start = true 你能告诉我我错过了什么吗? 谢谢 这是博客: – Chan 2010-07-28 19:19:43

0
  1. 创建单元测试项目
  2. 添加库项目,以您的解决方案。
  3. 右键单击库项目选择NuGet包选项。
  4. 搜索硒安装前两个选项硒和硒支持类。
  5. 同时下载chrome驱动程序,IEDriver,gecko驱动程序,如果你想执行跨浏览器测试。
  6. 在单元测试项目中创建测试类和测试方法。

    [识别TestClass] // ReSharper的禁用一次InconsistentNaming 公共类Test类 { [TestMethod的] 公共无效LoginTest() { //代码 } }

相关问题