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)