I try to run Nunit_selenium automation in updated firefox version,
vs 13,
selenium webdriver 3.8.0
NUnit3TestAdapter.3.9.0
Selenium.Support.3.8.0
Selenium.WebDriverBackedSelenium.3.8.0
Firefox launched but driver url link not updated
issue :in firefox notrun_image
please find the below code :
.
.
.
.
.
.
.
/
namespace SanityTesting
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework.Constraints;
using NUnit.Framework;
using NUnit.Core.Extensions;
using System.Configuration;
using System.IO;
using System.Reflection;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium;
using Syncfusion.UnitTesting.Framework;
using OpenQA.Selenium.Remote;
using System.Threading;
using OpenQA.Selenium.Support.UI;
using System.Collections;
using OpenQA.Selenium.Firefox;
using System.Diagnostics;
using System.Globalization;
using System.Drawing;
using SanityTesting.AccordionFluent;
using System.Windows.Forms;
[TestFixture("Firefox")]
[TestDirectory("..\\..\\Accordion\\")]
public class EmberJS_Accordion : NUnitUtil
{
string activeBrowser;
public EmberJS_Accordion(string browser)
{
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(#"C:\Drivers");
service.FirefoxBinaryPath = #"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
System.Environment.SetEnvironmentVariable("webdriver.gecko.driver", "C:\\Drivers\\geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver(service); // FirefoxDriver driver = new FirefoxDriver();
//driver = GetWebDriverForBrowser(browser);
//FirefoxProfile profile = new FirefoxProfile();
//profile.SetPreference("browser.startup.page", 0); // Empty start page
//profile.SetPreference("browser.startup.homepage_override.mstone", "ignore"); // Suppress the "What's new" page
// return new FirefoxDriver(profile);
activeBrowser = browser;
}
[TestFixtureTearDown]
public void quitDriver()
{
driver.Quit();
}
public void TakeAndCompareScreenshotByLocator(By Locator, string fileName)
{
IWebElement contents = driver.FindElementAfterClickable(Locator);
TakeAndCompareScreenshot(contents, fileName);
}
[Test, TestCaseSource(typeof(AccordionModel), "Sanity_Accordion")]
[Category("Sanity")]
[Component(Component.Accordion)]
public void EmberJS_Accordion_Sanity(string url, int time)
{
AutomationHelpers.GotoPage(driver, url);
driver.Navigate().Refresh();
driver.WaitForElementClickable(By.CssSelector(".e-acrdn"), time);
Thread.Sleep(200);
IWebElement contents = driver.FindElement(By.ClassName("cols-sample-area"));
TakeAndCompareScreenshot(contents);
}
}
change the following line of code,
FirefoxDriver driver = new FirefoxDriver(service);
to
driver = new FirefoxDriver(service);
I have doubt, is your NUnitUtil class have driver member variable. if yes, then above code may work.
Related
I need to execute addScriptToEvaluateOnNewDocument method of Google Headless Chrome in C# .Net. I basically need to execute Javascript before any other javascript is executed on the documents. Here is my code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
namespace User
{
class Program
{
//public static IWebDriver driver = new ChromeDriver();
public static IWebDriver driver;
public static double magnification = 2;
static void Main(string[] args)
{
ChromeOptions options = new ChromeOptions();
//options.AddArgument("Page.addScriptToEvaluateOnNewDocument");
options.AddArgument("--window-size=1920,1080");
options.AddUserProfilePreference("Page.addScriptToEvaluateOnNewDocument", "alert('INJECTED SCRIPT')");
driver = new ChromeDriver(options);
var action = new OpenQA.Selenium.Interactions.Actions(driver);
System.Threading.Thread.Sleep(1000);
driver.Navigate().GoToUrl("https://google.com");
System.Threading.Thread.Sleep(10000);
}
}
}
But the alert isn't fired (i have tried other JavaScript code besides alert - nothing seems to work)
Try this.After initialize the driver.Hope this will work.
driver.ExecuteJavaScript("alert(0);")
OR
driver.Scripts().ExecuteScript("alert(0);")
I have only done this in Python but it should work the same way.
The chromedriver should expose a function named execute_cdp_cmd()
You can use this to run JavaScript before the page JavaScript is run. See the python example below. Note that you should add the code shown below after creating the driver but before you get the web page.
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
alert(0);
"""
})
I don't have a C# example, but this might help.
using OpenQA.Selenium.DevTools;
using OpenQA.Selenium.DevTools.V91.Page;
using OpenQA.Selenium.Support.UI;
using RandomNameGeneratorLibrary;
using Network = OpenQA.Selenium.DevTools.V91.Network;
using DevToolsSessionDomains = OpenQA.Selenium.DevTools.V91.DevToolsSessionDomains;
IDevTools devTools = driver as IDevTools;
session = devTools.GetDevToolsSession();
var domains = session.GetVersionSpecificDomains<DevToolsSessionDomains>();
domains.Page.AddScriptToEvaluateOnNewDocument(new AddScriptToEvaluateOnNewDocumentCommandSettings()
{
Source = jscode,
});
I'm running a basic c# test below. My test is essentially going to go onto www.asos.com then search for a specific item. When the search results are loaded I want to click on the first item that's returned. I tried to use CSS selector to click on the first imagine but I get an exception. Invalid selector.
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.Support.UI;
using OpenQA.Selenium.Interactions;
using System.Threading;
namespace Exercise1
{
class Exercise_2
{
static void Main(string[] args)
{
IWebDriver webDriver = new ChromeDriver();
webDriver.Navigate().GoToUrl("http://www.asos.com/men/");
webDriver.Manage().Window.Maximize();
webDriver.FindElement(By.XPath(".//input[#data-testid='search-input']")).SendKeys("Polo Ralph Lauren Oxford Shirt In Regular Fit Blue");
webDriver.FindElement(By.XPath(".//button[#data-testid='search-button-inline']")).Click();
//*[#id="product-6153807"]/a/div[1]/img Xpath of the image to be clicked on
webDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
webDriver.FindElement(By.CssSelector("6153807")).Click();
}
}
}
use the following css selector.
article img
in code,
webDriver.FindElement(By.CssSelector("article img")).Click();
I am getting an exception System.ComponentModel.Win32Exception : The system cannot find the file specified when i employ Sikuli in C# using selenium.
I have referred to other posts in Stack but i have not not got a solution for that.
I am also running VS 2017 in administrator mode
I have latest copy of Rest-Api Jar in /Debug folder.
Below is my code
using System;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
using Sikuli4Net.sikuli_REST;
using Sikuli4Net.sikuli_UTIL;
using SikuliModule;
namespace DownloadFile
{
public class Download
{
private IWebDriver driver;
private APILauncher launcher;
[SetUp]
public void setup()
{
driver = new FirefoxDriver();
launcher = new APILauncher(true);
driver.Navigate().GoToUrl("https://java.com/en/download/");
driver.Manage().Window.Maximize();
}
[Test]
public void TestMethod1()
{
new WebDriverWait(driver, TimeSpan.FromSeconds(60)).Until(ExpectedConditions.ElementExists((By.XPath("//span[contains(text(),'Free Java Download')]")))).Click();
new WebDriverWait(driver, TimeSpan.FromSeconds(60)).Until(ExpectedConditions.ElementExists((By.XPath("//span[contains(text(),'Agree and Start Free Download')]")))).Click();
launcher.Start();
Screen screen = new Screen();
Pattern Click_Save = new Pattern("C:/Selenium Files/Save.jpg");
screen.Wait(Click_Save, 500);
screen.Click(Click_Save);
launcher.Stop();
}
}
}
Update your path of .jpg file in below line of code:
Pattern Click_Save = new Pattern("C:\\Selenium Files\\Save.jpg");
Rest of the code seems fine.
I get "Not Found" errors in Xamarin Forms with things like NSFileManager and NSUrl. These are my imports of my class where I'm getting these errors:
using System;
using System.IO;
using System.Reflection;
using DrinkUp.Controls;
using DrinkUp.iOS.Renderers;
using MediaPlayer;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
And the class which I'm using is located in DrinkUp.iOS.
Whenever and wherever I type something with the NS prefix it says that it could not find the object. Can someone please tell me what I'm doing wrong here?
Here's my full class
using System;
using System.IO;
using System.Reflection;
using DrinkUp.Controls;
using DrinkUp.iOS.Renderers;
using MediaPlayer;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(Video), typeof(VideoRenderer))]
namespace DrinkUp.iOS.Renderers
{
public class VideoRenderer : ViewRenderer<Video, UIView>
{
MPMoviePlayerController videoPlayer;
object notification = null;
void InitVideoPlayer()
{
var path = Path.Combine(NSBundle.MainBundle.BundlePath, Element.Source);
if (!NSFileManager.DefaultManager.FileExists(path)) {
Console.WriteLine("Video not exist");
videoPlayer = new MPMoviePlayerController();
videoPlayer.ControlStyle = MPMovieControlStyle.None;
videoPlayer.ScalingMode = MPMovieScalingMode.AspectFill;
videoPlayer.RepeatMode = MPMovieRepeatMode.One;
videoPlayer.View.BackgroundColor = UIColor.Clear;
SetNativeControl(videoPlayer.View);
return;
}
// Load the video from the app bundle.
NSUrl videoURL = new NUrl(path, false);
// Create and configure the movie player.
videoPlayer = new MPMoviePlayerController(videoURL);
videoPlayer.ControlStyle = MPMovieControlStyle.None;
videoPlayer.ScalingMode = MPMovieScalingMode.AspectFill;
videoPlayer.RepeatMode = Element.Loop ? MPMovieRepeatMode.One : MPMovieRepeatMode.None;
videoPlayer.View.BackgroundColor = UIColor.Clear;
foreach (UIView subView in videoPlayer.View.Subviews) {
subView.BackgroundColor = UIColor.Clear;
}
videoPlayer.PrepareToPlay();
SetNativeControl(videoPlayer.View);
}
}
}
The first comment worked, I had to get the NS types from Foundation.
Like Foundation.NSUrl etc. This fixed my problem.
The following is basically what i did. It is exactly what the example said but it does not go to the URL when I run it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace TestApp.Selenium_Basics
{
class TestExercise01
{
public void TestExercise01Run()
{
IWebDriver driver = new FirefoxDriver();
driver.Url = #"http://www.facebook.com";
}
}
}
The line below will not work:
driver.Url = #"http://www.facebook.com";
Try this instead:
driver.Navigate().GoToUrl(#"http://www.facebook.com");