Webdriver (selenium2.0) +nunit+c# (ii)

Source: Internet
Author: User
Tags xpath

Namespace seleniumtests{    [testfixture]    public class  login    {        private iwebdriver  driver;        private StringBuilder  verificationerrors;        private string baseurl;         private bool acceptNextAlert = true;                 [SetUp]         public void setuptest ()          {            driver = new  Firefoxdriver ();            baseurl =  " URL ";            verificationerrors = new stringbuilder ();         }                 [teardown]        public void teardowntest ()         {             try            {                 driver. Quit ();            }             catch  (Exception)              {                 // Ignore errors if unable to close the browser             }            assert.areequal ("",  verificationerrors.tostring ());        }                 [Test]         public void thelogintest ()         {             driver. Navigate (). Gotourl (baseurl +  "/login");             Driver. Findelement (By.name ("username")). Clear ();             driver. Findelement (By.name ("username")). SendKeys ("USERNAME");             driver.Findelement (By.name ("password")). Clear ();             driver. Findelement (By.name ("password")). SendKeys ("PASSWORD");             driver. Findelement (By.xpath ("//button[@type = ' submit ')"). Click ();         }     }  }

The

Above is a login action code for a page recorded with the Selenium IDE. The code for entering the user name and password is a bit repetitive, so you can encapsulate a method called SendKeys (including clear and sendkeys actions) without having to find the element each time, clear, Then repeat to find this element and then SendKeys. Similar to this commonly used operation can be encapsulated, placed in a common class (common project) and some of the operation case is placed in another project. Here's how to encapsulate the above example.

Namespace testselenium.test{    [testfixture]    class test     {        TestSelenium.Common.Common  Testcorde = new common.common ();        [setup]         public void setup ()          {            testcorde.setuptest ();         }        [teardown]         public void teardown ()          {             Testcorde.teardowntest ();        }         [test]    &nbsP;   PUBLIC VOID TEST01 ()         {                testcorde.thelogintest (" URL "," USERNAME "," PASSWORD " );                     }    }}

The

Above Test01 is the case,thelogintest of the login operation (string BaseURL, string Username, string password) is the method of the entire login operation. Like Setuptest, Teardowntest, SendKeys, Click, thelogintest are all placed under the common class below.

namespace testselenium.common{    public class common     {        public IWebDriver driver;         public void setuptest ()          {            driver = new  Internetexplorerdriver (@ "C:/auto");             Driver. Manage (). Window.maximize ();        }         public void teardowntest ()         {             driver. Quit ();         }        public  void sendkeys (By by, string message)        {             driver. Findelement (by). Clear ();             driver. Findelement (by). SendKeys (Message);        }         public void click (By by)         {             driver. Findelement (by). Click ();        }         Public void thelogintest (String baseurl, string username, string password)         {             driver. Navigate (). Gotourl (baseurl +  "/login");           &nbsP;   sendkeys (By.name ("username"), username);                   sendkeys (By.name ("password"),  password);             click (By.XPath ("//button[@type = ' Submit "]));         }    }}


The Ps.common project is the class library output type, and the case project needs to reference the common project and remains anycpu generated.

This article is from the "Xai" blog, make sure to keep this source http://chenxai.blog.51cto.com/10542839/1767936

Webdriver (selenium2.0) +nunit+c# (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.