Restricting browser drivers in selenium - java

I am a newbie to selenium. Just wanted to know how do I restrict the script to take only one version of a browser. I dont want to update my browser driver again and again. please help

Selenium usually takes the browser that is installed on your machine. So, I didn't understand this - 'restrict the script to take only one version of a browser'. Do you have multiple browser versions installed in your system?
You may follow the following approach:
Install the required version of browser alone in your local machine and don't update it.
Use Bonigarcia WebDriverManager - https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager with your Browser Version. Say, if you want to use chromedriver v88, you may set it to:
WebDriverManager.chromedriver().driverVersion("88").setup();
This will always use the same browser and driver version.

Related

Using selenium from within my app without any external server

From within my app I want to hit a website and than perform user action. Currently am using webview but I think webdriver will be easy to use and correct approach.
Current code:
WebView browser = (WebView) view.findViewById(R.id.webview);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setDomStorageEnabled(true);
browser.getSettings().setUserAgentString(`"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.81 Safari/537.36");`
browser.setWebViewClient(new MyBrowser());
browser.loadUrl("https://myurl.com");
Issues with current code: Its hard to send key stores or use Xpath.
What am looking for?
Hit the website using driver and than click buttons etc. Pseudo code as follows:
chat=driver.find_element_by_xpath("/html/somepath")
chat.click()
time.sleep(2)
search=driver.find_element_by_xpath("/html/body/div[1]/div/div/div[2]/div[1]/span/div/span/div/div[1]/div/label/input")
search.click()
I understand that this is possible using selenium/appium. But what am confused is does selenium/appium also needs a server that runs on a separate machine? I want to run all of the code in my app without external server or any more apps.
Can I just add lib which gives me access to apis like I showed above?
You can use the Selenium or Appium without any server. Both are plugins, which means they are basically open code or libraries. You call those objects on your local machine (or phone), you don't call an online remote API.
The Selenium and Appium helps to find elements on a web page or to find elements inside a mobile app. There is absolutely no need for a server here or remote machine.
So, YES, just add the lib which gives you access to api's like you showed above.
Selenium
Selenium in it's basic form doesn't needs any seperate server to run. Selenium along with it's wide range of tools and libraries that can support the automation of web browsers within the same machine ( i.e. localhost).
WebDriver
At the core of Selenium is WebDriver an interface to write instruction sets that can be run interchangeably in many browsers using each browser's native support for Test Automation. This can be achieved in three simple steps:
Installing the Selenium library
Installing the browser drivers
Writing your first Selenium script
Sample code block:
System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.com/");
driver.findElement(By.cssSelector("input.nav-input[value='Go']")).click();
Selenium Grid
However, Selenium also supports a distribution server for scaling browser allocation. If your usecase include steps to run tests in parallel across multiple machines, then Selenium Grid would be your best bet.
Selenium Grid allows the execution of WebDriver scripts on remote machines (virtual or real) by routing commands sent by the client to remote browser instances. It aims to provide an easy way to run tests in parallel on multiple machines.
Selenium Grid would also allow you to run tests in parallel on multiple machines and to manage different browser versions and browser configurations centrally (instead of in each individual test).
Having said that, it does solves a subset of common delegation and distribution problems, but will may not be able to manage your infrastructure, and might not exactly suit to your specific need.
Appium
Similarly, Appium is an open-source tool for automating native, mobile web, and hybrid applications on iOS mobile, Android mobile, and Windows desktop platforms. Hybrid apps which have a wrapper around a webview is a native control that enables interaction with web content. Projects like Apache Cordova make it easier to build apps using web technologies that are then bundled into a native wrapper, creating a hybrid app.
Based on your question and your response comment to the answer provided by #undetectedSelenium, the following assumptions apply:
You are testing a browser within an android phone that is connected to a Windows machine via an adb server running on the Windows machine
The browser under test is Chrome
Install selenium as part of your project:
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.2</version>
</dependency>
</dependencies>
Sample code block based on your psuedo code and answer provided by #undetectedSelenium
System.setProperty(“webdriver.chrome.driver”, “C:\\path\\to\\chromedriver.exe”);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption(“androidPackage”, “com.android.chrome”);
// By default if the following option is not applied, selenium will take the 1st available
// node provided by the adb server if multiple android devices are attached
options.setExperimentalOption("androidDeviceSerial", deviceId);
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.amazon.com/");
driver.findElement(By.cssSelector("input.nav-input[value='Go']")).click();
The deviceId variable needs to contain the uuid listed as a device from the adb server for the particular device under test, i.e.
options.setExperimentalOption("androidDeviceSerial", 95s572sp0478);
Also you will require the correct Chromedriver for your android device. Check the version of Chrome browser installed on the device and download the correct driver from here for your Windows machine Chromedriver Downloads. Then place into your desired directory and add the directory path into the code.

After updating the selenium jar and IE driver executable files. My script is running very slow

It is finding the element very fast but taking so much time to perform the actions like click or sendkeys. Currently updated files are Selenium Jar-3.7.1 and IEDriverExecutable-3.8.0
It is tough to answer/recommend anything with the information provided within the question currently. But using IEDriverExecutable-3.8.0 along with Selenium Jar-3.7.1 may be a potential issue.
The SeleniumHQ Download page says the following : The Internet Explorer Driver Server is required if you want to make use of the latest and greatest features of the WebDriver InternetExplorerDriver. Please make sure that this is available on your $PATH (or %PATH% on Windows) in order for the IE Driver to work as expected.
As per best practices, if you are using Selenium v3.7.x.y binaries try to use IEDriverServer v3.7.x.y binaries along with it.
Ensure that you have configured the mandatory settings as per InternetExplorerDriver page.
Finally it is worth to mention that your test execution speed will be slower while using the IEDriverServer 64-bit version with respect to test execution speed using IEDriverServer 32-bit version because of IE driver server 64 bit is typing very slowly using Sendkeys and IE x64 slow typing

chrome v62 enable flash

I have a selenium (v2.53) test that visits a site containing flash player (I'm testing this player). up until now, everything was working fine, but after I updated chrome to v62, flash is disabled by default.
I can't change manually flash setting since this test is automated and running on remote machines.
I've tried adding some chrome capabilities that should work on previous versions of chrome, but it did not work on chrome 62 version since allowing flash is not enough, now a list of allowed site is also required.
How can I change both the enabled status and the list of sites using selenium?
Also, is there a way to install chrome with a config file that both enables flash and populates the required sites list?
Thanks.
P.S. I'm working with Java 8
Your best bet is to simply use Chrome options. Why do you need a config file? That sounds overly complicated and unnecessary. You can enable it through chrome preferences. Try a fresh install of Chrome too.
Something akin to the likes of:
chromeOptions:{
args: ["--allow-running-insecure-content", "--allow-insecure-websocket-from-https-origin", "allow-outdated-plugins"]
You didn't specify which language so I can't give you a language example.

Cannot get Java Applets to work in any browser

We are trying to download something from GE that uses Java to download when logging into the site. This is a windows 7 Professional PC. I have other computers that are able to do this successfully. Here is my order of operations:
Log into the site and select the file I want to download
Click download
It takes me to the page that says it will start in a few seconds but nothing happens. It is supposed to have a box that asks for Java to run.
I have reinstalled Java fresh and still nothing. Tried with multiple user accounts. Added the site to the list of exceptions in the firewalls and Java configuration. I have tried an earlier version of Java. This happens in Mozilla, Chrome and IE. I have made sure that the Java plugin shows up and is enabled. I just cant think of what I am missing. And since we are a contractor GE is not going to help us. Can anyone here think of anything?
Are you sure you have the Java plugin enabled? e.g. in Firefox, go to Settings, Plugins, and change Java Platform SE8 'Next Generation Java Plugin' to 'always activate'?
This sounds similar to the issues I had with Cisco WebVPN, Java-style. Once you have Java installed correctly and set as a plugin for any of the browsers you would like to use, see below.
Before you even start looking at browsers - if you think it is already set up correctly
Look at your Anti-Virus programs or anything else that could prevent it from working. McAfee Host Intrusion Protection is known to cause many Java programs to fail. Kapersky had issues, a while back, with Java on Windows (Java Applets not loading in Windows 8 ).
You must have a 64-bit browser to use 64-bit Java (also mentioned in the Chrome link below).
See below for any specific things that can be modified in the browser.
Chrome 43 is the more complicated browser to set up. They have a dedicated page with instructions.
How do I use Java with the Google Chrome browser?
Firefox 38 will prompt you.
In Internet Explorer 11, it's under Internet Options->Security. I recommend adding the hostname the applet is on as a Trusted Site (Select Trusted Sites and click the Sites button, then add the first part of the url). Click the Custom level button and make sure that Scripting of Java applets is not disabled.
If you still have problems with the applet:
Verify your Java version will work with the applet you are accessing
Verify the plugin is enabled for the browser through the Java Control Panel, which is available in Windows Control Panel, or on Mac/Linux, execute it from the JDK directory ($JAVA_HOME/ControlPanel ).
I ended up fixing the issue. I had to allow their UK site on the list for Java and enable the SSL 2.0 for HTTP in Java config as well

Java Robot class not working from Jenkins

I am automating a scenario(selenium) where i need to enter credentials in Authentication Pop up, which is generated by the APP. I am using java Robot class to perform this function, it runs fine when i run it from my local machine which is a Windows Platform, but it doesn't seem to work when i trigger the build from jenkins. It hangs at a point where you need to enter the Credentials in Authentication Pop up. I think This is because the jenkins slave is an Unix Platform and this has something to do with the cross platform issue. As i am not sure if Unix Supports the Robot class.
Can somebody suggest me a workaround for this problem ?
Thanks in advance!!
The problem might be that you are testing on a headless server? Make sure your are not using firefox or chrome driver. Try to use the HtmlUnitWebDriver driver instead.

Categories

Resources