Visual Studio 2015 Selenium C# 'Using' Library NOT Found - selenium

OpenQA.Selenium.Support.UI.SelectElement NOT Found
As you can see by the code image, I have entered the 'using' library I need to use. However, it is telling me that "SelectElement" does not exist. Does anyone here know how to go about adding the correct library?
By clicking the above link, you will see the code image.
Here is the code snippet:
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Support.UI.SelectElement;
using NUnit.Framework;
namespace ToolsQA.Selenium_Basics
{
class DropDownAndSelectOperations
{
[Test]
public void Test()
{
// Create a new instance of the Firfox Driver
IWebDriver driver = new FirefoxDriver();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.
FromSeconds(10);
// Launch the URL
driver.Url = "http://toolsqa.wpengine.com/automation-practice-
form";
// Step 3: Select "Continents" drop down (Use Id to identify the
element)
// Find "Select" element of "Single Selection" using Id locator.
*SelectElement* oSelection = new
SelectElement(driver.FindElement(By.Id("continents")));

You need to add the Selenium Support package through NuGet. That should solve the problem.

Related

Is there a way to compare content of two text files using selenium?

Is there a way to compare the content of two text files using selenium?
I want to compare content of two files using automation.
I don't think you need Selenium for this as Selenium is a browser automation framework and browsers are mostly used for accessing web pages so I would recommend looking into file comparison solution for the programming language you're using for automation.
However if this is something you really need be aware that IWebDriver.Navigate().GoToUrl function can be used for opening local files on the file system as well.
Once you open the file you can read its contents into a variable using IWebDriver.PageSource property
Example code (assumes Selenium with C# using Chrome browser)
using OpenQA.Selenium.Chrome;
using System;
namespace selenium_csharp
{
class Program
{
static void Main(string[] args)
{
ChromeDriver driver = new ChromeDriver(ChromeDriverService.CreateDefaultService("c:/path/to/chromedriver/folder", "chromedriver.exe"));
driver.Navigate().GoToUrl("file://c:/somefolder/file1.txt");
var file1 = driver.PageSource;
driver.Navigate().GoToUrl("file://c:/somefolder/file2.txt");
var file2 = driver.PageSource;
if (file1.Equals(file2))
{
Console.WriteLine("Files are equal");
}
else
{
Console.WriteLine("Files are not equal");
}
driver.Quit();
}
}
}

How can I drag and drop to offset an element in a selenium test so that it will work on FireFox

When I run on FireFox browser a selenium test that is trying to move an element from its location to another using the function DragAndDropToOffset in Actions Class it fails with the following exception:
System.InvalidOperationException: data did not match any variant of untagged enum PointerActionItem at line...
When I'm trying to use DragAndDrop function that moves one element to the location of another instead of DragAndDropToOffset it works well.
I'm using Selenium.WebDriver and Selenium.Support of version=2.48.2.0
The code that I tried and faild:
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using Actions = OpenQA.Selenium.Interactions.Actions;
public void MyDragAndDropOffset(IWebElement source, int offsetX, int offsetY, RemoteWebDriver driver)
{
Actions actions = new Actions(driver);
actions.DragAndDropToOffset(source, offsetX, offsetY).Build().Perform();
}
I see that you are using CSharp to execute the tests. You can review details on the exception in the link: https://learn.microsoft.com/en-us/dotnet/api/system.invalidoperationexception?view=netframework-4.8

IllegalLocatorException - Selenium Web Driver

Ok I am writing a simple code in Selenium Web Driver. What it does is:
Open URL Google.com
Enter 'abc' in search bar
Click the Images tab
I am using windows 8 - 64 bit and Visual Studio 2013. Browser is Firefox.
Here is the code I wrote:
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.IE;
namespace WebDriverDemo
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Url = "http://google.com";
var searchBox = driver.FindElement(By.Id("gbqfq"));
searchBox.SendKeys("abc");
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromMinutes(1));
var images = driver.FindElements(By.ClassName("q qs"))[0];
images.Click();
}
}
}
But I am getting an exception on the second last line of the code. Here is the exception:
Here is the Inspect Element result:
The exception message tells you exactly what the problem is. You cannot use multiple, or "compound," class names when using By.ClassName. A single class name cannot contain a space. If you want to use multiple class names, use By.CssSelector.
And, the issue is compound class. Currently selenium does not support this. You can use the cssSelector on the other hand to avoid this issue.
.q.qs
Note . before each class and see my answer related to this question here
Complete code as per OP's update:
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.IE;
namespace WebDriverDemo
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Url = "http://google.com";
var searchBox = driver.FindElement(By.Id("gbqfq"));
searchBox.SendKeys("abc");
//The following line is missing that is mandatory.
driver.FindElement(By.Name("btnG")).Click();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromMinutes(1));
var images = driver.FindElements(By.CssSelector(".q.qs"))[0];
images.Click();
}
}
}
Using CSSSelector:
var images = driver.findElement(By.cssSelector(".q.qs"));
images.Click();
Using LinkText :
var images = driver.findElement(By.linkText("Images"));
images.Click();
Using Xpath :
var images = driver.findElement(By.xpath(".//*[#class='q qs' and .='Images']"));
images.Click();

Selenium: How to use captureentirepagescreenshot with remotedriver C#

In the Selenium docs I can see how to take a normal screenshot:
http://seleniumhq.org/docs/04_webdriver_advanced.html
But I would like to use captureentirepagescreenshot with a remote webdriver, C#
Is this possible? any examples?
Please use selenium webdriver version 2.33. Below code is a c# code so please download selenium webdriver dlls for .net from link http://docs.seleniumhq.org/download/
Selenium webdriver has inbuilt function called ITakesScreenshot to take screenshot runtime.
Please see below code
`public static void SaveScreenShot(string screenshotFirstName)
{
var folderLocation = "Screenshot folder path";
var screenshot = ((ITakesScreenshot)Driver).GetScreenshot();
var filename = new StringBuilder(folderLocation);
filename.Append(screenshotFirstName);
filename.Append(DateTime.Now.ToString("dd-MM-yyyy HH_mm_ss"));\\append date and time
filename.Append(".png"); \\append image extension
screenshot.SaveAsFile(filename.ToString(), System.Drawing.Imaging.ImageFormat.Png);
}`
Hope this will work for you.
Thanks,
Anshul
If you use a IWebDriver implementation (such as InternetExplorerDriver, FirefoxDriver) you can simply cast it to a ITakesScreenshot interface and use the GetScreenShot() method:
IWebDriver driver = new InternetExplorerDriver();
Screenshot screenShot = ((ITakesScreenshot)driver).GetScreenshot();
screenShot.SaveAsFile(/*fullFileNameAndPath*/, ImageFormat.Png);
But if you want to to do this with a RemoteWebDriver you can't, because it doesn't implement the ITakesScreenshot interface. So, you have to create a new class based on RemoteWebDriver that implements this interface and adds the GetScreenshot() method such as:
public class ScreenShotRemoteWebDriver : RemoteWebDriver, ITakesScreenshot
{
public ScreenShotRemoteWebDriver(Uri RemoteAdress, ICapabilities capabilities)
: base(RemoteAdress, capabilities) { }
/// <summary>
/// Gets a <see cref="Screenshot"/> object representing the image of the page on the screen.
/// </summary>
/// <returns>A <see cref="Screenshot"/> object containing the image.</returns>
public Screenshot GetScreenshot()
{
// Get the screenshot as base64.
Response screenshotResponse = this.Execute(DriverCommand.Screenshot, null);
string base64 = screenshotResponse.Value.ToString();
// ... and convert it.
return new Screenshot(base64);
}
}
Now you can use it as you would for a IWebDriver:
RemoteWebDriver driver = new ScreenShotRemoteWebDriver(/*uri*/, /*capabilities*/);
Screenshot screenShot = ((ITakesScreenshot)driver).GetScreenshot();
screenShot.SaveAsFile(/*fullFileNameAndPath*/, ImageFormat.Png);
It took me sometime to understand the maze of classes and interfaces for the drivers as well, but after a while it all makes sense.

How can I get the WebDriver version during Testrun?

I am writing on a testframework where the report should include the webdriver version of the test run. When using selenium there is the getEval("Selenium.version") method. But I find no way to read the version when using webdriver. Does anyone know a solution?
It's possible by reading the VERSION.txt properties file. This seems hacky, but it's what the WebDriver developers do in SeleniumServer.java:
final Properties p = new Properties();
p.load(getSeleniumResourceAsStream("/VERSION.txt"));
String rcVersion = p.getProperty("selenium.rc.version");
String rcRevision = p.getProperty("selenium.rc.revision");
String coreVersion = p.getProperty("selenium.core.version");
String coreRevision = p.getProperty("selenium.core.revision");
BuildInfo info = new BuildInfo();
String versionString = String.format("v%s%s, with Core v%s%s. Built from revision %s",
rcVersion, rcRevision, coreVersion, coreRevision, info.getBuildRevision());
Note that this requires a static import:
import static org.openqa.selenium.browserlaunchers.LauncherUtils.getSeleniumResourceAsStream;
Actual path to file with version in selenium is:
/META-INF/maven/org.seleniumhq.selenium/selenium-java/pom.properties
Properties p = new Properties();
p.load(LauncherUtils.class.getResourceAsStream("/META-INF/maven/org.seleniumhq.selenium/selenium-java/pom.properties"));
p.getProperty("version");`