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.
Related
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.
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.
I'm planning to use Selenium Chrome Driver for my project which will be used to do web scraping to multiple public websites (something like kayak or skyscanner). So there will be a REST GET endpoint where my backend would launch headless Chrome to scrape multiple websites, and eventually return a manipulated JSON.
I want to know how scalable is Chrome Driver as it sounds like a headless Chrome instance needs to be launched whenever a request comes in.
Updated: Question using Google Chrome Headless
Please find the pros and cons of phantom js which I noticed during implementation .Hope this helps.
Cons:
1)It will fail to recognize the browser elements like id,xpath,csselector
when compared to chrome driver.
2)If you have login mechanism ,redirects won't work as you expect when compared to chrome driver.
3)You need to manually implement the custom logic for screen shots for the test failures if you need it.
4)If you want to switch between multiple drivers like chrome,html etc then it is very difficult
Pros:
1)Test case execution is faster when compared to chrome driver
2)No browser is required it will run without GUI.
3)No much configurations are needed when compared to chromedriver.
You can go with html driver also which is quite faster then phantom but even it has its own limitations that you need take care of before implementation.
I am not sure that you really need to use PhantomJS.
Chrome implemented "headless" mode couple of months ago.
"Headless Chrome" does the same job that PhantomJS, and does it better.
I heard that PhantomJS authors even said that they will not support it anymore.
You can enable headless mode in Selenide with just on line:
Configuration.headless = true;
Did you think about headless chrome?
Headless Chrome
I got into a project which requires a WebGUI to be tested. Selenium is used for this in combination with Jenkins to schedule tasks and run the tests. All of this is handled by one server (Linux).
Regular tests work fine but WebGUI tests fail and I am supposed to make them work. However I'm very confused by the roles of Jenkins' Plugin "Selenium Grid" (is it even neccessary), the JUnit tests inside the project utilizing a selenium libary and the setup of the headless browser required.
What exactly are the roles of the plugin "Selenium Grid" for Jenkins and how does the Selenium libary communicate with a headless browser (Role of WebDrivers?)?
I already know about the Selenium hub and nodes but have no idea how to approach this.
I hope somebody can help me cleaning up my confusion...
A Selenium Grid can be used to set up a scalable automated browser environment. It is often used for automated GUI testing. It consists of a Hub and one or more Nodes.
A Selenium Hub should be started and the resulting URL should be stored.
Then a desired number of Selenium Nodes should be started with a reference to the Url of the Hub. The nodes will register themselves to the Hub.
Then you could use Selenium Java to create a RemoteWebDriver instance, optionally specifying the url of the Selenium Hub. This webdriver is just a Java API, the actual work will be done in the Selenium Node environment(s).
You can use the Java API to implement your desired operations on the WebDriver instance. When running, the code will delegate all calls to the running Selenium environment.
There are multiple ways to actually start the hub and nodes. A Jenkins Plugin could do it, you could use Docker, a local process, or many more. I'd advise to not bind your setup directly to Jenkins unless you are certain that you will never switch.
The Jenkins Selenium Plugin specifies the URL that you should create a WebDriver with:
new RemoteWebDriver(new URL("http://jenkins.mydomain:4444/wd/hub"), capability);
Where jenkins.mydomain will probably be the IP of your Linux server. Just above it is a line that says that it "also" accepts external nodes. I am not familiar with the plugin, so I dont know whether this means that it sets up one or more nodes itself by default.
taken from the following website.
https://wiki.jenkins.io/display/JENKINS/Selenium+Plugin
This plugin sets up Selenium Grid in the following way
On master, Selenium Grid Hub is started on port 4444, unless configured otherwise in Jenkins global configurations. This is where all your tests should connect to.
For each slave, necessary binaries are copied and Selenium RCs are started.
RCs and the Selenium Grid Hub are hooked up together automatically.
Now the question is do you need this?
This is upto the requirement of the project. You just need to ask this question to yourself
do you need to run tests in parallel (to cutdown time or for browser compatibility testing), either now or in near future.
I'm using the Java API of the Selenium WebDriver:
Is it possible to create multiple instances of the Selenium WebDriver from different threads simultaneously without conflict?
How do I change the path of the firefox installation directory that WebDriver uses if I installed firefox in a different directory?
How can I hide all the instances of the browsers(e.g firefox) that those threads started?
Thank you.
I can give you an answer to your first question.
Yes, you can run multiple driver instances simultaneously. However it is not recommended to run more than 5 or so instances at once in a single selenium server. Selenium Grid was designed specifically for this (it is bundled with the Selenium Server).