Can't load html from local file in PhantomJS - c#

Using Selenium and PhantomJS, I can load a file from a server running on localhost like this:
_driver.Url = "http://localhost:52873/Home/Template";
Where _driver is an instance of RemoteWebDriver that connects to an instance of PhantomJS running on a port (launched with --webdriver=9134). And I can call a Javascript function defined in that HTML file like so:
var val = _driver.ExecuteScript("myFunction()");
However, I would like to load it from the local file system rather than through the local webserver, so I tried something like this:
_driver.Url = "file://c:/path/to/Home/template.html";
An I get no errors, but when I try to execute a script that is defined in the html file, it does not work. It throws an InvalidOperationException with the message Can't find variable: myFunction. However, if I set _driver to an instance of ChromeDriver instead, I can see the page load and everything works as expected, both using http and file protocols.
I get no additional information from the PhantomJS console window, so I don't really know what the problem is. I suspect for some reason PhantomJS just isn't loading the page. Any ideas?
Also, is there any way using the .NET binding for selenium to get access to the console log? I added a console.log("connected") to my html file to run as soon as the page loads, but it doesn't get echoed to the phantomJS console either when run from file or from http, so that's not much help.

Looks like you are missing an extra / in the path URI.
_driver.Url = "file:///c:/path/to/Home/template.html";

Related

How to identify chromedriver.exe?

I have some RPA processes which use ChromeDriver to connect to specific instances of Chrome identified by its remote-debugging-port and a custom command arument for chrome which idenitifies the specific chrome instances as "from my program".
I have a program using Selenium ChromeDriver. I do not let ChromeDriver create the Chrome Instance, but rahter re-use an existing one. So when my program starts up it can find the specific Chrome I want to use, and connect a new ChromeDriver to it. The issue is that sometimes the ChromeDriver is left running stuck because I exited without Quitting it (this is development work - stuff happens!)
I would like to be able to somehow tag the ChromeDriver that was launched by my programs. Iti is easy to tage the Chrome instance - I just pass in any unique argument and it happily accepts it, as in "--originator=me", then I can go through the chrome processes, get their command line and know which Chrome is mine. But I do not know how to do this with ChromeDriver. Here are ideas I have that have not worked:
Pass in a command line argument to chromedriver, but the only argument I see in the process is "port=12345"
Get the Process ID of the chromedriver I just created. But I do not know how to ask the new driver for this, even though I have access to it.
Any ideas on how to identify a chromedriver that my process created, assuming that later on my program will be looking for this specific instance?
You can use a ChromeDriverService to create a ChromeDriver then give the service a LogPath. That LogPathcan be the identifier as long as it's a valid file name. Just like this:
var svc = ChromeDriverService.CreateDefaultService();
svc.LogPath = LogPath
ChromeOptions options = new ChromeOptions();
driver = new ChromeDriver(svc, options);
Then if you want to find the specific ChromeDriver, just find the windows process with this command-line argument. In this case we really don't care about the contents of that file itself, just the fact that the magic word is now programmatically discoverable.

C# Selenium, can't find any elements after logging in

I use Selenium with ChromeDriver. When I open up a driver and manually go to a webpage, and then start running my code, everything works fine. However, if I log in to the website and manually go to the same page as before (it's the exact same page but logged in), I won't even find the html element:
var el = driver.FindElement(By.CssSelector("html"));
returns
OpenQA.Selenium.WebDriverException: The HTTP request to the remote WebDriver server for URL http://localhost:........../elements timed out after 60 seconds.
The assignment
var el = driver.FindElements(By.CssSelector("*"));
returns the same thing, there's literally nothing. Shouldn't I at least get a "cannot find element" exception?
Now if I log out it works again (everything is done in the same instance of ChromeDriver)

Chrome driver is not able to pickup a file from the system when we run it through a Service

When I run Selenium Grid (Hub and Node) through a windows service and try to get a file from my system in my test cases through chrome driver, it shows "File not found on the path" at the sendkeys function of Selenium Webdriver, also if I do a File.Exist in the service the file is available and when I run it normally without service it is able to pick up the file with the same code.
Note: When we run test cases through service Web page and chrome is not visible and when we run normally web page and chrome is visible.
I am using Selenium Webdriver with C#.
public void FileUpload(IWebDriver driver,string pathOfFolder, string attachmentFile)
{
var filePath = Path.Combine(pathOfFolder, attachmentFile);
if(File.Exists(filePath))
{
driver.FindElement(By.XPath("//input[#type='file']")).SendKeys(filePath);
}
}
File is picked up from a shared drive(T:):
filepath = "T:\SampleFolder\FileUpload.xlsx";
The shared drive and the service have all the required permissions(I have used administrator permissions).
The problem arises when I do sendKeys with the path it gives file not found exception.

How to upload image to web page in Saucelabs test by Selenium in c#?

I must upload any image to web page to create online shop to move on test. I have to click on button for uploading after that I have to give directory of the file, but I can't give local directory because test runs on VM on Saucelabs. How to solve this problem?
Use the LocalFileDetector class. Your code will look something like this:
// WARNING!! Untested code written from memory, without benefit of an IDE.
// May not work exactly correctly or even compile without modification.
// Assume driver is a properly instantiated IWebDriver object, which is
// to be used with a remote service (including SauceLabs or similar).
IAllowsFileDetection fileDetectionDriver = driver as IAllowsFileDetection;
fileDetectionDriver.FileDetector = new LocalFileDetector();
IWebElement fileElement = driver.FindElement(By.Id("idOfFileInputElement"));
fileElement.SendKeys(#"C:\path\to\local.file");
By setting the file detector, the SendKeys method will first upload the file from your local system to the remote machine on which the code is actually running. Then SendKeys will set the file in the <input> element, using the local file path on the remote machine.

cross browser testing using codedui?

i have issue in cross browser testing using codedui.
Using below code,
Process.Start("firefox", url);
BrowserWindow.CurrentBrowser = "firefox";
Browser = BrowserWindow.Launch(new System.Uri(url));
Keyboard.SendKeys("^{0}");
all code developed in IE . but now i have to execute code in firefox or chrome.I am going to execute the code in forefox.I am using this code here
Browser = BrowserWindow.Launch(new System.Uri(url));
in this line getting error like "An error occurred while connecting to Firefox".how to resolve this issue?I installed selenium components also. if i remove this line I am getting diffrent error like " Unable to find browser"...Please help.
Out of the box Visual Studio doesn't support cross browser CodedUI testing.
You're going to need to install Selenium components to allow for cross browser testing in Visual Studio.
Details on that can be found here:
http://blogs.msdn.com/b/visualstudioalm/archive/2012/10/30/introducing-cross-browser-testing-with-coded-ui-tests.aspx
Selenium components can be found here:
http://visualstudiogallery.msdn.microsoft.com/11cfc881-f8c9-4f96-b303-a2780156628d
Looks like CodedUI doesn't support playback on very many different browsers http://msdn.microsoft.com/en-us/library/dd380742(VS.100).aspx
There are some other tools out there http://watin.org/ is one, but I can't find anything myself that will really solve your problem.
Try
BrowserWindow.CurrentBrowser = "firefox";
BrowserWindow WebApp;
WebApp.CopyFrom(BrowserWindow.LaunchUrl(new System.Uri(url)));
I set up something similar to the following and it works well (I hand code everything, bypassing the UIMap).
public class WebApp : BrowserWindow
{
private string _url;
public WebApp(string url)
{
//define search properties using this keyword so the web application can be treated as a browser
_url = url;
BrowserWindow.CurrentBrowser = "Chrome";
this.CopyFrom(BrowserWindow.Launch(new Uri(url));
}
}
You can overload the constructor, of course, by adding a parameter for the browser to use or whether to start up the browser or not.
By setting up the web application as a BrowserWindow, you can have one open and ready and the playback engine should find it. I find this helps when working on tests (in IE).
Just a reminder, you do need the Selenium plug-in and that plug-in will only work for playback, not recording.
Cheers!
In our environment, we set up the Launch() method by doing the following:
public void LaunchBrowser(string uri)
{
BrowserWindow.CurrentBrowser = "firefox";
BrowserWindow myBrowser = BrowserWindow.Launch(new System.Uri(uri));
}
One thing to note is that if there is a firefox process already running in the background, the WebDriver will not launch a new instance, so be sure that all instances of firefox are shut down before the LaunchBrowser() is called. I've found that there are Java plugins that can keep it running in the background, so try to disable those that you don't need. Another good place to look, if you check your Task Manager and this is the case, is here.

Categories