How to run Selenium Brave web browser webdriver in c#? - webdriver

Someone kind can help me with this.
I have a problem and it is that I cannot run Selenium in Brave with visual studio. Can someone help me on the code? What would it be to execute it? Because with the visual studio webdriver automatically running chrome, I want to tell it to run Brave. Thank you

Requirements:
Install Nuget Packages (Selenium.Webdriver, Selenium.Chrome.Webdriver)
private string chromeDriverPath = #"C:\Path\To\chromedriver.exe"; //Path to chromedriver.exe
private string braveBrowserPath = #"C:\Path\To\brave.exe"; //Path to brave.exe
private ChromeOptions chromeOptions; //Default value of chromeOptions is null
private IWebDriver driver; //Default value of driver is null
public void LoadChrome() {
try
{
Environment.SetEnvironmentVariable("webdriver.chrome.driver", chromeDriverPath); //Sets process evnironment
chromeOptions = new ChromeOptions(); //Initialize chromeOptions
chromeOptions.BinaryLocation = braveBrowserPath; //Sets the location of Brave
driver = new ChromeDriver(chromeOptions); //Initialize driver with chromeOptions
driver.Manage().Window.Maximize(); //Set Brave window maximized
driver.Navigate().GoToUrl("https://www.google.com/"); //Loads page Google
} catch (Exception ex) {
Console.WriteLine(ex.Message); //Print any errors to console log
}
}
Selenium seems to be pretty easy to use. You can take a look at their API and Documentation that include usages and examples with C# programming language.
There is also a cheat sheet you can use for reference in case you're too lazy to look up API or Documentation.

Related

dotnet core TopShelf Windows Service fails to start

I have a dotnet core console application build to connect to a Sql Service Broker instance to monitor table changes.
The app monitors one table that is updated from an ERP system and then publishes messages to our bus.
It runs fine when running as a console application, or debugging in my IDE.
I am having an issue when using TopShelf to configure it as a windows service.
Here is the entry point:
private static void Main(string[] args)
{
RegisterComponents();
var serviceHost = HostFactory.Run(sc =>
{
sc.Service<ISalesOrderMonitorService>(s =>
{
var sqlListener = _container.ResolveNamed<SqlDependencyEx>(ListenerKey.SalesOrder);
var changeHandler = _container.Resolve<ISalesOrderChangeHandler>();
var listenerConfig = _container.ResolveNamed<ListenerConfiguration>(ListenerKey.SalesOrder);
var logger = _container.Resolve<ILogger<SalesOrder>>();
s.ConstructUsing(f =>
new SalesOrderMonitorService(sqlListener, changeHandler, listenerConfig, logger));
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
});
var exitCode = (int) Convert.ChangeType(serviceHost, serviceHost.GetType());
Environment.ExitCode = exitCode;
}
The "worker" class:
public abstract class ServiceBase<T, TZ> : IService<T>
where T : IChangeHandler
{
protected readonly IChangeHandler ChangeHandler;
protected readonly SqlDependencyEx Listener;
protected readonly ListenerConfiguration ListenerConfiguration;
protected readonly ILogger<TZ> Logger;
protected ServiceBase(SqlDependencyEx listener, IChangeHandler changeHandler,
ListenerConfiguration listenerConfiguration, ILogger<TZ> logger)
{
Logger = logger;
ListenerConfiguration = listenerConfiguration;
Listener = listener;
ChangeHandler = changeHandler;
}
public virtual void Start()
{
try
{
Listener.TableChanged += (o, e) => ChangeHandler.Process(e);
Listener.Start();
Logger.LogDebug(
$"Listening to changes on the {ListenerConfiguration.Table} table in the {ListenerConfiguration.Database} database");
}
catch (Exception e)
{
Logger.LogError(e, e.Message);
throw;
}
}
public virtual void Stop()
{
Listener.Stop();
}
Install through TopShelf is no problem:
c:>{ServiceName}.exe install -username "serviceAccount" -password "superSecret" -servicename "ServiceName" -servicedescription "Description" -displayname "Service DisplayName" --autostart
When I go to start the service - I get this:
This is misleading because the event viewer shows this:
This is happening way faster than 30 seconds. This is definitely related to how I am configuring TopShelf.
As stated - the application works just fine when run "debug" or even as just an exe console.
I got it figured out. Actually both comments from #DotNetPadawan and #Lex Li indirectly got me there.
For starters - enabling the remote debugger clued me in that my appsetting.json was not being read into my IConfiguration. That was really confusing because everything works fine running locally with a debugger or even just starting the exe.
The link Lex Li points out did not provide the answer - however that article had this reference:
Host and Deploy aspnetcore as a Windows Service
It was here that I found this little nugget:
The current working directory returned by calling GetCurrentDirectory for a Windows Service is the C:\WINDOWS\system32 folder. The system32 folder isn't a suitable location to store a service's files (for example, settings files). Use one of the following approaches to maintain and access a service's assets and settings files.
The link explains how to conditionally set the current directory if the app is running as a service.
var isConsole = args.Contains("-mode:console");
if (!isConsole)
{
var pathToExe = Process.GetCurrentProcess().MainModule?.FileName;
var pathToContentRoot = Path.GetDirectoryName(pathToExe);
Directory.SetCurrentDirectory(pathToContentRoot);
}
Putting this out there for anyone else that runs into this problem.
Admittedly - netcore 3.0 is likely the better way to go - but I don't have the bandwidth to upgrade everything is this repo (lots of shared stuff) to 3.0. I needed to get this working.

Can't find a working example of SQLite.Net-PCL in Xamarin Forms

I can't seem to get off dead center with SQLite.Net-PCL; I have a Xamarin forms solution with a PCL proj, Droid proj, iOS proj, and Windows Universal project. I have installed the nuget packages for SQLite.Net-PCL and SQLite.Net.Core-PCL.
So, I go over to the github project to find some awesome examples to get started and none of them work; apparently you have to pass a platform into the database connection, but I get a reference error on them all (such as SQLitePlatformWin32).
Search the web for the references and... nothing. Search nuget for the platforms and... nothing.
What am I missing? (yes, I feel dumb)
An example they have is
public class Database : SQLiteConnection
{
public Database(string path) : base(new SQLitePlatformWin32(), path)
{
CreateTable<Stock>();
CreateTable<Valuation>();
}}
and I get a reference error that I can't resolve on the "new SQLitePlatformWin32" line.
For anyone else struggling, here is what you need to do.
Install the SQLite.Net-PCL, SQLite.Net.Core-PCL and SQLite.Net.Async-PCL nugets
Create an interface for ISQLite in your PCL:
public interface ISQLite
{
SQLiteConnection GetConnection();
}
Call GetConnection via DependencyService
PatientDatabase = DependencyService.Get<ISQLite>().GetConnection();
PatientDatabase.CreateTable<Patient>();
The above will create the connection based on your platform (i.e. android, ios). In each platform's project, you must have a GetConnection that is accessable via DependencyService such as this for Android
[assembly: Xamarin.Forms.Dependency(typeof(SQLite_Android))]
// ...
public class SQLite_Android : ISQLite
{
public SQLite_Android() { }
public SQLite.Net.SQLiteConnection GetConnection()
{
var sqliteFilename = "TodoSQLite.db3";
string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); // Documents folder
var path = Path.Combine(documentsPath, sqliteFilename);
// Create the connection
var conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid(), path);
// Return the database connection
return conn;
}
}
My problem was I was trying to create the connection in my PCL and I couldn't find the platforms, what you need to do is create the connection in each individual project.

How to change the language of a WebDriver?

I want to execute my Selenium tests in different languages. Is it possible to change the language of an existing WebDriver at runtime or do I have to recreate the browser instance?
Right now I'm only using Firefox, but I want to execute tests in different browsers at some later point.
In Firefox I set the language like this:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("intl.accept_languages", "de");
WebDriver driver = new FirefoxDriver(profile);
I also implemented a WebDriverPool, which holds a WebDriver instance so it can be shared among tests. If the language can only be set at creation time, I could hold an instance for every locale.
All in all I wonder if I miss something here. Why is it so hard to change the language? shouldn't there be a method like WebDriver.setAcceptLanguages(Locale)?
In a nutshell I have these questions:
Why isn't there WebDriver.setAcceptLanguages(Locale)?
How to change the language for the dirrerent WebDrivers?
Can I change the language at runtime?
How did you guys implement your WebDriverPool or do you recreate them everytime?
I ended up creating a WebDriverPool that creates one instance for every combination of WebDriver type (e.g. FirefoxDriver.class) and Locale (e.g. en_US). Maybe this is usful for someone.
public class WebDriverPool {
private Map<String, WebDriver> drivers = new HashMap<String, WebDriver>();
private List<WebDriver> driversInUse = new ArrayList<WebDriver>();
public WebDriverPool() {
Runtime.getRuntime().addShutdownHook(new Thread(){
#Override
public void run(){
for (WebDriver driver : drivers.values())
driver.close();
if (!driversInUse.isEmpty())
throw new IllegalStateException("There are still drivers in use, did someone forget to return it? (size: " + driversInUse.size() + ")");
}
});
}
private WebDriver createFirefoxDriver(Locale locale){
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("intl.accept_languages", formatLocale(locale));
return new FirefoxDriver(profile);
}
private String formatLocale(Locale locale) {
return locale.getCountry().length() == 0
? locale.getLanguage()
: locale.getLanguage() + "-" + locale.getCountry().toLowerCase();
}
/**
* #param clazz
* #param locale
* #return web driver which can be new or recycled
*/
public synchronized WebDriver getWebDriver(Class<? extends WebDriver> clazz, Locale locale){
String key = clazz.getName() + "-" + locale;
if(!drivers.containsKey(key)){
if(clazz == FirefoxDriver.class){
drivers.put(key, createFirefoxDriver(locale));
}
// TODO create other drivers here ...
// else if(clazz == ChromeDriver.class){
// drivers.put(key, createChromeDriver(locale));
// }
else{
throw new IllegalArgumentException(clazz.getName() + " not supported yet!");
}
}
WebDriver driver = drivers.get(key);
if(driversInUse.contains(driver))
throw new IllegalStateException("This driver is already in use. Did someone forgot to return it?");
driversInUse.add(driver);
return driver;
}
public synchronized void returnWebDriver(WebDriver driver){
driversInUse.remove(driver);
}
}
You can also do it through about:config in firefox. But you need to use Actions to manipulate it.
Below a java piece of code
Actions act = new Actions(webDriver);
webDriver.get("about:config");
// warning screen
act.sendKeys(Keys.RETURN).perform();
// Go directly to the list, don't use the search option, it's not fast enough
act.sendKeys(Keys.TAB).perform();
// Go to the intl.accept_languages option
act.sendKeys("intl.accept_languages").sendKeys(Keys.RETURN).perform();
// fill the alert with your parameters
webDriver.switchTo().alert().sendKeys("fr, fr-fr, en-us, en");
webDriver.switchTo().alert().accept();
I am afraid that the whole idea of WebDriver is to act like browser - so you can change the language of the browser, but you have to change the locale in the Operating system, or hope that the application will do it for you.
For instance - German number format separates decimal number by comma and English one by dot. If you want to test, how the number format behaves in English locale and in German locale, you can do it only by these two approaches:
Change OS locale from German to English or vice versa
Change browser language and hope that application will change the behavior.
To answer your questions:
There is no setLocale on Webdriver, because WebDriver simulates browser, not OS
I would do it like this (Java code):
private WebDriver driver;
public enum Language {en-us, de}
public WebDriver getDriver(Language lang){
String locale = lang.toString();
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("intl.accept_languages", locale);
driver = new FirefoxDriver(profile);
return driver;
}
#Test
public void TestNumber(){
WebDriver drv = getDriver(Language.en);
drv.get("http://the-site.com");
WebElement el = drv.findElement //... find element
String number = el.getText();
Assert.assertEquals(number, "123.45");
drv.close();
drv = getDriver(Language.de);
drv.get("http://the-site.com");
WebElement el = drv.findElement //... find element
String number = el.getText();
Assert.assertEquals(number, "123,45");
drv.close();
}
I am afraid you have to close the browser and open it again with different language.
I personally create new instance of the browser for each test.
BTW the above bit of code assumes, that the web application changes the way how to show numbers to the user based on browser language.

Unable to connect to host 127.0.0.1 on port 7055

I am a newbie with webdriver and need some help..
I am using Selenium 2.2.0 with FF v7.0.1 on Windows XP
I've managed to record and play back a java script successfully in IE but whenever I try and execute the same script in FF, I get the following error message:
Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms
I've read at numbe of places that if I downgrade the firefox version to 3.6 script will work fine however I am not to keen on downgrading. Can someone please tell me what I am doing wrong?
package hisScripts;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WebdriverTest_1 {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
//driver=new InternetExplorerDriver();
baseUrl = "https://**********/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void testUntitled() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.xpath("//a[contains(text(),'my profile')]")).click();
driver.findElement(By.xpath("//a[contains(text(),'about the service')]")).click();
driver.findElement(By.xpath("//a[contains(text(),'contact us')]")).click();
driver.findElement(By.xpath("//a[contains(text(),'help')]")).click();
driver.findElement(By.xpath("//a[contains(text(),'home')]")).click();
driver.findElement(By.xpath("//a[contains(text(),'logout')]")).click();
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}
The selenium version you are using is extremely old. I don't think firefox 10 is supported in v2.2. The latest is 2.20.
Take a look at change log here. From the notes here native events in firefox 10 were supported starting from v2.19.0 that means you would need 2.19 or higher to support firefox 10.
This problem is due to the compatibility of fire fox version and the selenium jar file version.Use the latest selenium jar files.that can fix the problem.

rso between flex and red5. I can create but cant read

so im still stuck on this that i can create remote shared object but cant read while im notified about changes. im using trunk version of red5, and flex 4.0 with flash builder. in debug i can see that changeLog has name of the changed value of rso, but object itself has undefined value.
Currently im working on local windows machine, but tried everything on ubuntu server 10.04 and got the same results. i can connect to the room, create a shared object and all client are notified about that, but only the user who changed the value of rso can read the value that one time, other just get undefined value.
Does anybody has any experience with this issue? I would really appreciate any help, because this is just driving me crazy, im for about three weeks, read all tutorials about rso and cant get any solution. I tried with persistent and non-persistent, initiated by server and by client, but all the time get the same results.
there is my code on the client side:
protected function application1_creationCompleteHandler(event:FlexEvent):void {
var room_id:Number = vars("room");
connection = new NetConnection();
connection.connect("rtmp://127.0.0.1/video/" + room_id);
connection.addEventListener(NetStatusEvent.NET_STATUS, onConnected);
connection.client = this;
}
private function onConnected(event:NetStatusEvent) : void {
if(event.info.code == "NetConnection.Connect.Success") {
so = SharedObject.getRemote("video", connection.uri, true);
so.addEventListener(SyncEvent.SYNC, onSync);
so.connect(connection);
} else {
Alert.show("Unsuccessful Connection", "Information");
}
private function onSync(event:SyncEvent):void {
if(so.data["video"] != undefined)
Alert.show(so.data["video"].toString(), "Information");
}
on the server side i have:
ISharedObject so;
IServiceCapableConnection iconn;
public static IScope iroom;
/** {#inheritDoc} */
#Override
public boolean connect(IConnection conn, IScope scope, Object[] params) {
iconn = (IServiceCapableConnection)conn;
if (!super.connect(conn, scope, params)) {
return false;
}
System.out.println("Connected True");
return true;
}
/** {#inheritDoc} */
#Override
public void disconnect(IConnection conn, IScope scope) {
super.disconnect(conn, scope);
}
#Override
public boolean roomStart(IScope room) {
if (!super.roomStart(room))
return false;
createSharedObject(room, "video", true);
so = getSharedObject(room, "video");
System.out.println("Room created succesfully");
ISharedObjectListener listener = new SOEventListener();
so.addSharedObjectListener(listener);
return true;
}
with listener on the client side i cant make output in console and see that rso is changed and what is current value, although im checking the persistence rso file on red5 server and the that look like everything is working and the only thing what is missing is opportunity to read value for all clients.
I will appreciate any help. Thanks
big problem appears not such a big. Problem was with encoding, which is AMF3 by default since AS3 and all i need to do, just change the encoding to AMF0.
connection = new NetConnection();
connection.objectEncoding = ObjectEncoding.AMF0;
connection.connect("rtmp://127.0.0.1/video/" + room_id);
Hope that helps for anybody, because somehow there is not a lot information about things like these on the net.

Resources