Selenium C# tests in Browserstack - c#

Does anyone have experience with running Selenium C# tests in Browserstack. Trying out this example from the Browserstack, but I can´t seem to get the test to the Test explorer in Visual Studio. Not sure why I´m not able to execute the test. Any ideas? I´m having no problems running my local test in Visual Studio.
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
namespace SeleniumTest
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver;
DesiredCapabilities capability = DesiredCapabilities.Chrome();
capability.SetCapability("browserName", "iPad");
capability.SetCapability("platform", "MAC");
capability.SetCapability("device", "undefined");
capability.SetCapability("browserstack.user", "");
capability.SetCapability("browserstack.key", "");
driver = new RemoteWebDriver(
new Uri("http://hub-cloud.browserstack.com/wd/hub/"), capability
);
driver.Navigate().GoToUrl("http://www.google.com");
Console.WriteLine(driver.Title);
IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("Browserstack");
query.Submit();
Console.WriteLine(driver.Title);
driver.Quit();
}
}
}

Try changing this line:
driver = new RemoteWebDriver(new Uri("http://hub-cloud.browserstack.com/wd/hub/"), capability
To
driver = new RemoteWebDriver(new Uri("http://hub-cloud.browserstack.com/wd/hub/"), capability, TimeSpan.FromSeconds(600));
If this does not work, debug it and find out where it is failing so we can narrow it down. You are using the user and key correct?

using System;
using System.Security.Policy;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
namespace SeleniumTest
{
[TestClass]
class Program
{
[TestMethod]
public void Test()
{
IWebDriver driver;
DesiredCapabilities capability = DesiredCapabilities.Chrome();
capability.SetCapability("browserName", "iPad");
capability.SetCapability("platform", "MAC");
capability.SetCapability("device", "undefined");
capability.SetCapability("browserstack.user", "");
capability.SetCapability("browserstack.key", "");
driver = new RemoteWebDriver(
new Uri("http://hub-cloud.browserstack.com/wd/hub/"), capability);
driver.Navigate().GoToUrl("http://www.google.com");
Console.WriteLine(driver.Title);
IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("Browserstack");
query.Submit();
Console.WriteLine(driver.Title);
driver.Quit();
}
}
}
**Change the add this code and try to check it by adding in empty Unit test class
file**

Related

C# Automation Edge Browser - using Edge Driver - auto testing program - Failure: No matching capabilities found (SessionNotCreated)

Greetings stackoverflow community,
I am trying to compile and run the programcode from this website:
https://social.msdn.microsoft.com/Forums/en-US/7bdafd2a-be91-4f4f-a33d-6bea2f889e09/c-sample-for-automating-ms-edge-chromium-browser-using-edge-web-driver
I followed all the instructions listed in the link and set my paths were I wanted them.
The program and the edge driver starts running, but then an error appears.
"An error exeption "System.InvalidOperationException" appeared in WebDriver.dll.
Further Inforamtion: session not created: No matching capabilities found (SessionNotCreated)"
This is the code from my program, more or less copied from the link above:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var anaheimService = ChromeDriverService.CreateDefaultService(#"C:\edgedriver_win64", "msedgedriver.exe");
// user need to pass the driver path here....
var anaheimOptions = new ChromeOptions
{
// user need to pass the location of new edge app here....
BinaryLocation = #"
C: \Program Files(x86)\Microsoft\Edge\Application\msedge.exe "
};
IWebDriver driver = new ChromeDriver(anaheimService, anaheimOptions); -- error appears at this line
driver.Navigate().GoToUrl("https: //google.com/");
Console.WriteLine(driver.Title.ToString());
driver.Close();
}
}
}
I would really appreciate your help!
Best Regards
Max
The article you refer to is a bit out of date. Now we don't need to use ChromeDriver to automate Edge. You can refer to the official doc about how to use WebDriver to automate Microsoft Edge.
I recommend using Selenium 4. Here I install Selenium 4.1.0 NuGet package and the sample C# code is like below:
using System;
using OpenQA.Selenium.Edge;
namespace WebDriverTest
{
class Program
{
static void Main(string[] args)
{
var options = new EdgeOptions();
options.BinaryLocation = #"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
var driver = new EdgeDriver(#"C:\edgedriver_win64", options);
driver.Navigate().GoToUrl("https://www.google.com");
Console.WriteLine(driver.Title.ToString());
driver.Close();
}
}
}

IE in Private Mode using Selenium C#

I want to open IE in Private mode to run the set of test cases. The browser is not opening. It shows error as
The HTTP request to the remote WebDriver server for URL {URL} timed out after 60 seconds
Sample code:
InternetExplorerOptions options = new InternetExplorerOptions()
{
ForceCreateProcessApi = true,
BrowserCommandLineArguments = "-private",
};
IWebDriver driver = new InternetExplorerDriver("C:\\Reports", options);
driver.Navigate().GoToUrl("https://www.google.com");
Also I have changed the TabProcGrowth as 0 in Registry Editor.
How to open IE in private mode to run the test case? Anything I want to update in my code. Thanks in advance.
This is how I manage to launch it:
Set the TabProcGrowth as 0 in Registry Editor.
Get the Selenium.WebDriver.IEDriver64 nugget instead of the normal 32 and build the project
Get the IEDriverServer64.exe from bin\Debug\netcoreapp3.1 (the output folder where this file is generated depends on your TargetFramework: .netcore or .netstandard)
Rename that file into IEDriverServer.exe and put it somewhere in a folder
Create the driver instance using the path to that folder. In my case, I created a folder in the project and pointed there
Project: Solution Explorer View
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using System.IO;
namespace InternetExplorerPrivate
{
public class Tests
{
public IWebDriver driver;
[SetUp]
public void Setup()
{
InternetExplorerOptions options = new InternetExplorerOptions();
options.ForceCreateProcessApi = true;
options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
options.BrowserCommandLineArguments = "-private";
driver = new InternetExplorerDriver(Path.GetFullPath(#"..\..\..\IEDriver"), options);
}
[Test]
public void Test1()
{
driver.Navigate().GoToUrl("https://stackoverflow.com/");
}
}
}

Selenium C# Not able to redirect after the click for login

I met problems of not able to log in with selenium. I think the login button is clicked successfully. But the website that I am testing against has some login-check or rest-assured Ajax handling which I don't know. So after I click the login button on the website, I am not able to log in and redirect. I am not sure what's going on.
The website I am practising is https://www.catch.com.au/.
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System.Threading;
using System;
namespace Selenium429ErrorDemo
{
public class Tests
{
string url = "https://www.catch.com.au/";
private IWebDriver _driver;
[Test]
public void Test1()
{
_driver = new ChromeDriver();
_driver.Navigate().GoToUrl(url);
_driver.Manage().Window.Maximize();
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(30));
_driver.FindElement(By.CssSelector("[data-testid='myaccount-reference']")).Click();
_driver.FindElement(By.Id("login_email")).SendKeys("EMAIL");
_driver.FindElement(By.Id("login_password")).SendKeys("PASSWORD");
IWebElement btn = _driver.FindElement(By.Id("button-login"));
wait.Until(ExpectedConditions.ElementToBeClickable(btn));
btn.Click();
IWebElement ert = _driver.FindElement(By.CssSelector(".css-n11h0l"));
wait.Until(ExpectedConditions.UrlMatches("https://www.catch.com.au/"));
}
}
}

Selenium C# Can't find ID or Title

I manage to open a firefox browser, go to http://www.google.com/ search for "Bath Fitter". When i see a bunch of links, i want to in fact click on an item of the top menu provided by Google, Images. Images is located next to Map Videos News...
How can i have it click on Images?
Below 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 SeleniumHelloWorld
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = null;
try
{
driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.google.com/");
driver.Manage().Window.Maximize();
IWebElement searchInput = driver.FindElement(By.Id("gbqfq"));
searchInput.SendKeys("Bath Fitter");
searchInput.SendKeys(Keys.Enter);
searchInput.FindElement(By.Name("Images"));
searchInput.Click();
driver.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception ****" + e.ToString());
}
}
}
}
More specifically you can also write your selector pointing from Top Navigation. This is the XPath.
.//*[#id='hdtb_msb']//a[.='Images']
try this;
driver.FindElement(By.XPath(".//*[#id='hdtb_msb']//a[.='Images']"));
EDIT:
Even though the selectors above were correct your code was not working because of the second page was taking too long to load. There you need to wait for the the element to be in ready state and an implicit wait is needed. Change the code in your try block and replace with mine and try
driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.google.com/");
driver.Manage().Window.Maximize();
IWebElement searchInput = driver.FindElement(By.Id("gbqfq"));
searchInput.SendKeys("Bath Fitter");
searchInput.SendKeys(Keys.Enter);
//this is the magic
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
By byImage = By.XPath(".//*[#id='top_nav']//a[.='Images']");
IWebElement imagElement =
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists(byImage));
imagElement.Click();
Try something like this...
IList<IWebElement> links = driver.FindElements(By.TagName("a"));
links.First(element => element.Text == "Images").Click();

How to use Selenium Grid with C#?

I'm researching Selenium and have a seminar for my group...
I meet many troubles with this
I use C# language and write a demo SeleniumExample.dll Then I start
selenium RC and NUnit and run it with NUnit to see the test report.
I read testdata from XML.
Here's the SeleniumExample.dll: using System;
using System.Xml;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using Selenium;
namespace SeleniumExample
{
public class Success_Login
{
//User defined
private string strURL = "http://newtours.demoaut.com/
mercurywelcome.php";
private string[] strBrowser = new string[3] { "*iehta",
"*firefox", "*safari" };
// System defined
private ISelenium selenium;
private StringBuilder verificationErrors ;
[SetUp]
public void SetupTest()
{
selenium = new DefaultSelenium("localhost", 4444,
this.strBrowser[0], this.strURL);
selenium.Start();
verificationErrors = new StringBuilder();
}
[TearDown]
public void TeardownTest()
{
try
{
selenium.Stop();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
Assert.AreEqual("", verificationErrors.ToString());
}
[Test]
public void Success_LoginTest()
{
try
{
XmlDocument doc = new XmlDocument();
XmlNode docNode;
doc.Load("XMLFile1.xml");
docNode = doc["TestCase"];
foreach (XmlNode node in docNode)
{
selenium.Open("/");
selenium.Type("userName",
node["username"].InnerText);
selenium.Type("password",
node["password"].InnerText);
selenium.Click("login");
selenium.WaitForPageToLoad("30000");
Assert.AreEqual("Find a Flight: Mercury Tours:",
selenium.GetTitle());
}
}
catch (AssertionException e)
{
verificationErrors.Append(e.Message);
}
}
}
}
Now I want to have a demo that using Selenium Grid (SG) but I don't know
how to do. I read document and understand the way SG works. I install
SG and install Ant1.8. The tests will communicate with Selenium Hub.
Actually, I just understand the theory I don't know how to make the
tests communicate with Selenium Hub and how to make Selenium Hub
control Selenium RC.
I am a new for Selenium. If anyone know this, please help me. I
appreciate it so much.
THANKS,
Hoang
In reality there is no major difference between Selenium RC and Selenium Grid. The only difference is that with Grid you don't have to know where the RC nodes are but if you use Selenium RC you will have to.
string hubAddress = "machineNameWithSeleniumGridHub"
[SetUp]
public void SetupTest()
{
selenium = new DefaultSelenium(hubAddress, 4444,this.strBrowser[0], this.strURL);
selenium.Start();
verificationErrors = new StringBuilder();
}
When your tests run they will speak to the hub which will then push the commands out to the first available RC. I have a Selenium Tutorial on my site. It uses C# and should get you going.

Categories