How to click chrome extension using selenium webdriver java? - selenium

I am able to load a chrome extension through CRX file and successfully add it to the chrome with selenium webdriver using java with Chrome Options, can anyone tell me how to automate to click the extension once it added to the chrome.
Every time i have to click manually on the extension for the further process of automation.

Not sure how you are doing it. Try loading the extension using ChromeOptions, it loads the browser with extension and there is no need to click any button manually
ChromeOptions options = new ChromeOptions();
//Adding Chrome extension
options.addExtensions(new File("Chrome extension - crx file path"));
options.addArguments("--start-maximized");
options.addArguments("--test-type");
System.setProperty("webdriver.chrome.driver", "path to/chromedriver.exe");
driver = new ChromeDriver(options);
Just FYI - handling OS level controls is out of reach of Selenium.
To handle OS level controls you can use AutoIT or LDTP.
Personally I prefer LDTP for smaller interaction as it gives seamless experience and is platform independent - https://github.com/ldtp/cobra
You can also try Pywinauto, a python library. With pywinauto you write the code to handle the OS level control and call this python script from Java code. For most of the tools you need to install their executables but for Pywinauto you only need Python to be installed which is approved to use in most of the organizations. For basic pywinauto script you need not learn the language.
Refer https://pywinauto.readthedocs.io/en/latest/

Related

Using webdriver plugin in jmeter

Iam trying to use webdriver plugin to automate an application in jmeter. Can anyone help me to do scripting in selenium by using webdriver plugin. Thanks in advance.
It depends on the browser and programming language you're using.
Currently Chrome is the most popular browser and JavaScript is the default language for the WebDriver Sampler so:
Download chromedriver (make sure that the version you download matches your Chrome browser version)
Add Chrome Driver Config and specify the path to the chromedriver in the "Chrome" tab:
Add WebDriver Sampler and implement the code for your test scenario. WDS.browser will stand for the ChromeDriver instance so you will be able to: use
WDS.browser.get('some url') - open a page
var element = WDS.browser.findElement(org.openqa.selenium.By.id('some id')) - locate a WebElement
element.click() - click the element found in the previous step
etc.
More information: The WebDriver Sampler: Your Top 10 Questions Answered

Clear browser cache and history with Selenium WebDriver

I use Selenium WebDriver 3.141.59 with ChromeDriver and try to remove browser cache and history during test execution.
I have found several solutions about using chrome://settings/clearBrowserData page in test scripts, but these seems flaky.
In v4.0, there will be a better solution to interact with DevTools, but it isn't yet released.
driver.getDevTools().createSessionIfThereIsNotOne();
driver.getDevTools().send(Network.clearBrowserCookies());
Whenever chrome opens it stores info in a profile folder. The best solution might be to create a custom profile folder every time you start the driver.
ChromeOptions chromeProfile = new ChromeOptions();
chromeProfile.addArguments("user-data-dir=" + chromeProfilePath);
WebDriver driver = new ChromeDriver(chromeProfile);

Start safari with extension using safariDriver in selenium

I want to start my browser with my extensions. In chrome I can use the chromeOptions as in code sample below. Firefox works in a similar way.
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
However in Safari there is no addExtensions method. What is the correct way to do this with Safari?
This might help.
https://github.com/SeleniumHQ/selenium/wiki/SafariDriver
I found that if you want to open another safari extension inside safari, you have to do it from the safari browser itself.
https://github.com/SeleniumHQ/selenium/wiki/SafariDriver-Internals#building-the-safaridriver
So basically:
Sign up for Apple's (free) Safari Developer Program and generate a
signed certificate for the extension.
Build the SafariDriver extension: $ ./go safari
Install the extension:
Launch Safari
Enable the Develop menu (Preferences > Advanced > Show Develop menu in menu
bar)
Open the Extension Builder (Develop > Show Extension Builder)
Add a new extension: $SELENIUM_CLIENT/build/javascript/safari-driver/SafariDriver.safariextension
Click Install
Which version of Selenium are you using? It looks like they added safariOptions.addExtensions(".."); in June of 2013.
SafariOptions options = new SafariOptions();
options.addExtensions(new File("path/to/extension.safariextz"));
WebDriver driver = new SafariDriver(options);
The Apple-maintained safaridriver implementation does not support configuring extensions via WebDriver capabilities or other automated means. If you feel that this is an important feature worth doing, please file an enhancement request at https://bugreport.apple.com/ with more details of your use case.
SafariDriver is a class in the org.openqa.selenium.safari package is used to access safari browser
WebDriver driver = new SafariDriver();
driver.get("https://stackoverflow.com");

Robot Framework browser support

Has the robot framework support for IExplorer or only for Firefox and Chrome?
(If yes, how to configure it?)
Thanks!
Robot Framework does not, in itself, support any particular browser, so I am guessing you are referring to either SeleniumLibrary or Selenium2Library which use selenium and selenium 2 respectively. The browser support of these is well documented at seleniumhq and there is much support out there. It is recommended for new projects to use Selenium2Library as this will receive ongoing support.
Please check the driver compatiblity for browser.
You might have already known of IE driver.
Apart from that you also need to check Python version- Selenium2 version - IE Driver version - IE browser version compatibility.
In addition to #theheadofabroom 's answer, I should add that Internet Explorer does not play well with Robot Framework. Your test might not work for any number of reasons on IE while it may work just fine on FireFox and Chrome, but the most common is timing. IE is just slow enough that when Robot Framework goes to click on the next element, it searches the page for it, but it hasn't loaded in yet. As long as you have the Selenium webdriver for IE installed correctly and have written your Robot Framework code correctly, I'd recommend adding some Sleep keywords between actions to slow your code down and increase the probability that the element you want to click will load before Robot Framework searches the page for it. This is especially true if you're writing for Chrome and want to send it to either Firefox or IE.
Open Browser ${WEBAPPURL} ${BROWSER} is the keyword to open the browser.
For Firefox you can use firefox/ff instead of ${BROWSER}
For Google Chrome you can use googlechrome/gc/chrome instead of ${BROWSER}
For Internet Explorer you can use internetexplorer/ie instead of
${BROWSER}
For Firefox you don't need any driver but IE and Chrome you need to install the drivers
You can find the installers in and info here for Chrome and here for IE
Download IEdriver exe from here and put this exe file in Scripts folder of your Python installation directory. For eg, in my case it is C:\Python27\Scripts.
Ride will now launch IE for you.
Robot class supports keyboard inputs regardless of the browser. It is a class from the java.awt package and not specific to any browser. It is used in automation for performing operations on the web browser(stand alone application) in which a web-page is being automated
Note that it cannot perform operations directly on the web browser as it's a stand alone application, but can make use of keyboard shortcuts to indirectly perform the operation.
For example, if you want to open a new tab in a browser, you can use the Robot class to press Ctrl+t instead of trying to click on the new tab.
Code to use it to open a new tab in your program
Webdriver driver = new ChromeDriver(); //FirefoxDriver(), IntrrnetExplorerDriver();
driver.get("......");
//code goes here
//to open a new tab
Robot rob = new Robot();
rob.keyPress(Keys.VK_CTRL);
rob.keyPress(Keys.VK_t);
rob.keyRelease(Keys.VK_CTRL);
rob.keyRelease(Keys.VK_t);
//itetator to switch between the tabs

Setting up Selenium for recording as well as running in different browsers and platforms

I'd like to setup a Selenium server so that clients can record tests locally, recorded tests can be replayed and tested on an Ubuntu server with Firefox + Chrome.
Unfortunately the Selenium site is so confusing and mentions so many different projects (Selenium 1, Selenium 2, Selenium RC, Selenium Grid) that I'm not sure where to start.
How do I go about setting up Selenium Server on an Ubuntu box?
Unfortunately the Selenium site is so
confusing and mentions so many
different projects (Selenium 1,
Selenium 2, Selenium RC, Selenium
Grid) that I'm not sure where to
start.
Selenium has multiple versions
IDE - mainly to record the test and play it back. It is mainly a Firefox Addon. This can be used for very basic testing. You can also export the recorded test to selenium RC. All these mentioned in seleniumhq.org->documentation section: http://docs.seleniumhq.org/docs/
RC - Like any other automation tool, you can write your own code to run the test rather than just recording and playing it back. This has far better capabilities than IDE including support for several languages (Java, Javascript, Ruby, PHP, Python, Perl and C#) and support for almost every browser out there in various platform.
Grid - This helps in running multiple tests in parallel.
To record and run the test in Firefox (NOT CHROME) its very easy. This doesn't require a selenium server running.
record the whole test
save it in a file
Copy the file to Ubuntu machine
Open the same test using IDE in Ubuntu machine and run it again in firefox
If you want to run on chrome, then you need to go to the next level of using selenium RC. And this requires the selenium server running.
How do I go about setting up Selenium
Server on an Ubuntu box
Download selenium-server jar from here. Copy this to any directory in your ubuntu server
Open a terminal and navigate to the folder which has the selenium server jar.
Enter java -jar selenium-server-jarfilename.jar
Selenium server will start on port 4444 by default and keep listening to the tests.
The site is confusing in terms of the versioning and names. Selenium is the name of the entire project which started off as Selenium RC (remote control). Selenium RC is the old version of the API which is also sometimes called Selenium 1. Selenium 2 is the newest version and the latest release was last week being Selenium RC2 (release candidate). This uses a different API to Selenium RC. The new API is known as WebDriver. The new API still allows you to access the older Selenium RC but only for backwards compatibility.
Since you're starting now, there is no reason for you to use the Selenium RC API. You should instead use the advanced user interactions which are part of WebDriver. Setting up WebDriver is pretty easy and there is a decent guide on it here. You should note that the API used there is the older standard (2.0 beta) which uses WebElements. The new API (advanced user interactions) decouples actions from the elements they are performed on a lot more. I would recommend you use the latest versions of the API which is being actively supported rather than older deprecated versions.
Since you want to do this all locally, the second link I gave you should be enough to get you up and running. Assuming you're going to be using the Java bindings it is as simple as:
public class Selenium2Example {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
Actions builder = new Actions( driver );
builder.sendKeys( driver.findElement( By.name("q") ), "Cheese!" );
Action action = builder.build();
action.perform();
//Close the browser
driver.quit();
}
}
This is the example code edited to use advanced user interactions.
You must have two things to write and execute selenium tests.
1) Selenium Server is also known as Selenium RC (Remote Control). You can go to this link and download Selenium Server.
You can start selenium server with command java -jar ur_selenium_server.jar
2) Client Driver: Using client-driver you can code selenium tests. It consists of combination of selenium commands that perform certain actions on the UI. For e.g. click, select etc.
Selenium supports many different language bindings for client-driver. Download appropriate client-driver for your preferred language from above download page.
You can refer client driver apis and code your tests.
Hope this helps