开发者

How to set up Selenium to work with Visual Studio .NET using C#?

开发者 https://www.devze.com 2023-01-09 12:02 出处:网络
I tried to google, but there are many different ways to work with Selenium. I\'m using: - Windows 2003 Server

I tried to google, but there are many different ways to work with Selenium. I'm using: - Windows 2003 Server - Visual Studio 2008 - Selenium IDE installed through Firefox - NUnit 2.5 is copied into C:\ - Selenium RC is copied into C:\

  1. First I created a Library Project using C#.
  2. And this my class :
using System;
using System.Text;
using System.Text.RegularExp开发者_如何学JAVAressions;
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. Next add all references from the C:\Selenium RC\selenium-dotnet-client-driver-1.0.1
  2. Compiled the Library Project, succeeded. No errors.
  3. Run NUnit.exe, now errors :(

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


You are getting the Forbidden error because you are setting the baseURL to that of Selenium RC. You need to set it to http://www.google.com and then in your test would look like

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

or you need to change your test to

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


to setup ide for selenium in conjunction with c# is to use visual studio express. And you can nUnit as the testing framework. Below links provide you more details

How to setup C#,nUnit and selenium client drivers on VSExpress for Automated tests

Creating Basic Selenium web driver test case using Nunit and C#


  1. Create Unit testing project
  2. Add library project to your solution.
  3. right click on library project select NuGet package option.
  4. search selenium install first two options selenium and selenium support class.
  5. Also download chrome driver, IEDriver, gecko driver if you want to perform cross-browser testing.
  6. create the test class and test method in the unit testing project.

    [TestClass] // ReSharper disable once InconsistentNaming public class Test class { [TestMethod] public void LoginTest() { // code } }

0

精彩评论

暂无评论...
验证码 换一张
取 消