I am trying to launch my Selenium Launch when a button is press on my windows form. I am unsure on how to link the class library with the Windows Form. I am getting stuck because for the script/class to run correctly the project Output Type must be set to "Class Library" otherwise it gives out loads of errors.
Here is the class I am trying to launch :
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumTest
{
[TestClass]
public class SeleniumTest
{
[TestMethod]
public void TestMethod1()
{
// Set what browser to use
ChromeDriver driver = new ChromeDriver(#"C:\Users\Alex\Documents\Selenium");
// Set the base website
string baseURL = "http://kd.svr-webdev-01.df.local";
driver.Navigate().GoToUrl(baseURL + "/");
driver.Close();
}
}
}
All you need to do is put your unit test (TestMethod1) body into a button click event. Easiest way to make that event is just to drop a button onto the form using the designer and double click it.
Visual Studio will create a blank event for you, and you just need to copy/paste your current code into there.
Even, to make it simpler, create a console application and stick that body into the the "Main" method.
Related
I'm using VS 2015 community. My Selenium C# test case always runs twice. The Test Case Explorer window shows 1 test case was run but the pass result shows two of the same test cases were executed.
What is wrong with my test or framework?
I have created a Test File with testcase (NunitDemo.cs) under my project.
I attached a screenshot to my Solutions Explorer window as well.
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace DemoNunit
{
[TestFixture]
public class NunitDemo
{
private IWebDriver driver;
[Test]
public void tc_newAccount()
{
//open browser and navigate to aut
driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.trainingrite.net");
//click on signup button
driver.FindElement(By.CssSelector("input.submitbtn")).Click();
//enter firstname, lastname, email, password
driver.FindElement(By.Id("ctl00_MainContent_txtFirstName")).Clear();
driver.FindElement(By.Id("ctl00_MainContent_txtFirstName")).SendKeys("Ren");
driver.FindElement(By.Id("ctl00_MainContent_txtLastName")).Clear();
driver.FindElement(By.Id("ctl00_MainContent_txtLastName")).SendKeys("G");
driver.FindElement(By.Id("ctl00_MainContent_txtEmail")).Clear();
driver.FindElement(By.Id("ctl00_MainContent_txtEmail")).SendKeys("rmng3#yahoo.com");
driver.FindElement(By.Id("ctl00_MainContent_txtPassword")).Clear();
driver.FindElement(By.Id("ctl00_MainContent_txtPassword")).SendKeys("12345");
driver.FindElement(By.Id("ctl00_MainContent_txtVerifyPassword")).Clear();
driver.FindElement(By.Id("ctl00_MainContent_txtVerifyPassword")).SendKeys("12345");
driver.FindElement(By.Id("ctl00_MainContent_txtHomePhone")).Clear();
driver.FindElement(By.Id("ctl00_MainContent_txtHomePhone")).SendKeys("951-265-1234");
driver.FindElement(By.Id("ctl00_MainContent_txtCellPhone")).Clear();
driver.FindElement(By.Id("ctl00_MainContent_txtCellPhone")).SendKeys("760-855-1234");
driver.FindElement(By.Id("ctl00_MainContent_txtInstructions")).Clear();
driver.FindElement(By.Id("ctl00_MainContent_txtInstructions")).SendKeys("Running first selenium automation scripts in C#!");
//click on submit button
driver.FindElement(By.Id("ctl00_MainContent_btnSubmit")).Click();
//verify new customer is added successfully
Assert.AreEqual("Customer information added successfully", driver.FindElement(By.Id("ctl00_MainContent_lblTransactionResult")).Text);
}
}
}
Do you have both the Nunit 2.x and 3.x Test Adapter installed in VS? If so try removing one of them and running the test.
As an addition to Dmitry's answer, if you have the NUnitTestAdapter installed via the Extensions as well as via the NuGet package, the tests will run twice. This is a known issue.
I exported some code from Selenium IDE into my C# page in Visual studio 2013 and it keeps giving me this error. I just copied the steps from the exported document into my c# page. The platform finds the LinkText on the actual page but it keeps giving me this error.
I changed it to find the element by id but, then I don't know how to select the menu item in webpage.
I will really appreciate your help with this.
Thank you
I get this error:
{"Unable to locate element: {\"method\":\"link text\",\"selector\":\"My Resume\"}"}**
This is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace MyFirstAutoScript
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("https://yelitzascareerjourney.wordpress.com/");
driver.FindElement(By.LinkText("My Resume")).Click(); //my page brakes here
more commands go here...
}
}
}
You must have to mouseover first on menu item "About Me" before
clicking on sub menu item "My Resume". And you can use Actions commands from Selenium for same.
I don't have much idea about C# but your code would be something like this:
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.Interactions;
namespace MyFirstAutoScript
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("https://yelitzascareerjourney.wordpress.com/");
new Actions(driver).MoveToElement(driver.FindElement(By.LinkText("About Me"))).Click().Build().Perform();
driver.FindElement(By.LinkText("My Resume")).Click(); //my page brakes here
more commands go here...
}
}
}
I had used new Actions(driver).MoveToElement(driver.FindElement(By.LinkText("About Me"))).Click().Build().Perform(); to mouse over on menu item.
I want to use SikuliIntegrator in C#.
I run VS as administrator, install SikuliIntegrator throught NuGet manager and want to test him on simple task.
Heres my code
using SikuliModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SikuliTrainingNet
{
class Program
{
static void Main(string[] args)
{
string MyPicture = #"c:\111\Sik\MyPicture.png";
SikuliAction.Click(MyPicture);
}
}
}
after running code (and have prepared MyPicture on screen), all i get is exception "###FAILURE" any idea why?
I dont wanna use Sikuli4Net, becose its look like it work on web aps and I need just few simple clicks on desktop aplication.
I try sikuli in Java, and there it works with no problem. But I need to make my program in C#.
I Used This Code For Sikuli4Net in C#,It Was Working For Me First You need add the References please see this link for Reference
http://interviews.ga/angularjs/sikulic/
static void Main(string[] args)
{
APILauncher launch = new APILauncher(true);
Pattern image1 = new Pattern(#"C:\Users\Ramesh\Desktop\Images\userName.png");
Pattern image2 = new Pattern(#"C:\Users\Ramesh\Desktop\Images\password.png");
Pattern image3 = new Pattern(#"C:\Users\Ramesh\Desktop\Images\Login.png");
launch.Start();
IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Url = "http://gmail.com";
Screen scr = new Screen();
scr.Type(image1, "abc#gmail.com", KeyModifier.NONE);
scr.Type(image2, "12345", KeyModifier.NONE);
scr.Click(image3, true);
Console.ReadLine();
}
I used this code and it was working fine. First you should open the webpage on which you want to click and then give a path of the image(it should be a part of the webpage)
here is my code:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SikuliModule;
using OpenQA.Selenium;
namespace WordPressAutomation.DifferentTests
{
[TestClass]
public class Sikuli
{
[TestMethod]
public void TestMethod1()
{
driver.Initialize();
driver.instance.Navigate().GoToUrl("https://www.google.co.in");
SikuliAction.Click("E:/img.png");
}
}
}
To use SikuliInyegrator, you need to check the execution results in these files:
C:\SikuliExceptionLog.txt
C:\SikuliOutputLog.txt
Also you need to:
Have installed JRE7 or superior
Have environment variable PATH with the location of the bin folder
See installed in your “control panel > program and features> visual C++ 2010 Redistributable Package” at x86 and x64 bits according to your java JRE runtime platform. If not, then download and install the Redistributable Package form Microsoft site.
I have almost searched every forum/website related to Selenium WebDriver but still unable to find the solution that how to user Assertions and Verifications in Selenium WebDriver using C#.
Here is my code where I just want to put a sample assertion in the code written below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
namespace Healthfleet
{
class Login
{
static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver(#"D:\Downloads");
driver.Navigate().GoToUrl("https://test.com/");
IWebElement username = driver.FindElement(By.Id("username"));
IWebElement password = driver.FindElement(By.Id("password"));
username.SendKeys("test#test.test");
password.SendKeys("test");
IWebElement loginButton = driver.FindElement(By.Id("Login"));
loginButton.Click();
}
}
}
I need to check if username = test or not by using assertion but I am unable to find any Assert class or method.
Am I missing some namespace which contains the Assert class or any other idea?
NUnit was required to test this. And you have to add its dll and then add namespace as
using NUnit.Framework;
Currently you've written the program that manages the browser. If you'll add assertion's from NUnit - it will throw an exceptions in case of failure.
If you want to create tests you should create class without static void Main(string[] args) but add some methods marked with [Test].
I would recommend you to learn the concepts of xUnit-systems and NUnit in particular.
I have written a simple Form1 (inherit from Form) class in Visual Studio C#. All was good.
Then I wanted to change the name of the class and the namespace to something meaningful instead of the default 'WindowApplicationForm1' . I also changed the file name of the Form1.cs class to match the new class name (IRISReaderGUI and Com.Harmonysoft). First I manually rename the code, then compiled, the compiler gave me lot of error that I could not figure out. So I tried to rename my class and namespace using 'refactoring' menu. My code still didn't compile.
I did some research and change the 'IRISReaderGUI.Designer.cs' class name and namespace to match the new names. C# still didn't give me no joy.
The compiler erros message was :
'Com.Harmonysoft.IRISReaderGUI.Dispose(bool)': no suitable method
found to override'
On the designer view it said : The base class'System.object' can not be designed.
So I guess my IRISReaderGUI did not correctly inherit the System.Windows.Forms.Form class, this was confirmed by putting the mouse over the Form word in the code, Visual Net does not popup the text describing the class, and pressing F12 does not get me to the Form definition.
Here is the code :
"IRISReaderGUI.Designer.cs"
namespace Com.Harmonysoft
{
partial class IRISReaderGUI
{
.......
}
"IRISReaderGUI.cs"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Data.Odbc;
using System.Data.OleDb;
using System.Data.OracleClient;
using Com.StellmanGreene.CSVReader;
using System.Windows.Forms;
using System.IO;
namespace Com.Harmonysoft
{
public partial class IRISReaderGUI : Form
{
......
}
I am new to C# and Visual Studio, I have previously worked with Java compiler and Vim editor mostly. Could anyone please help me to compile my code ?
Forms in VS2005 and up are partial classes. You edited the one part but not the other. In the Solution Explorer window, expand the node next to the form and double-click the Designer.cs file to open it.
Using the Refactor + Rename context menu command is the better approach, it doesn't forget to edit the other source files as well. It didn't work when you tried it because the damage was already done, the parts no longer had the same name.