I'm trying to set up my first very simple UX test using Vaadin TestBench. To avoid the headache of downloading drivers and setting System.properties or PATH values, I'm also using the WebDriverManager library.
To make it a little trickier, our login page is a JSP that we will need to open and authenticate before being able to test the Vaadin application.
Here is the simple test I've been trying:
public class LoginIT extends TestBenchTestCase {
private static final String URL="http://localhost:8080/";
#Before
public void setup() throws Exception {
ChromeDriverManager.getInstance().setup();
setDriver(new ChromeDriver());
}
#After
public void tearDown() throws Exception {
if (getDriver() != null) {
getDriver().quit();
}
}
#Test
public void testLogin_success() {
getDriver().get(URL);
Assert.equals(URL, getDriver().getCurrentUrl());
WebElement usernameField = driver.findElement(By.name("username"));
}
}
The simple test above will pass on the currentUrl assertion. However, it fails to find the element.
I think I have two issues here.
The browser doesn't open/navigate to the URL. Chrome doesn't open a new tab/page when running the test if Chrome is already open. Alternatively, if I allow it to launch the browser it does so without opening a page (on Mac OSX) so I never can visually confirm it navigates to the URL.
I've tried with Firefox, which apparently has a lot of issues with Selenium, and PhantomJS, which has issues with a missing .lib file in the latest binary. With the WebDriverManager, I downgraded to PhantomJS 2.0 but it times out waiting for http://localhost:29436/status.
If it does "successfully" navigate to the URL, as Chrome says it does, it isn't able to find the element. This might be due to number 1?
If TestBench isn't able to handle the JSP login, then it will be useless for my application. Any help is greatly appreciated. What could I be doing incorrectly that is causing my issues?
Created a simple test example for this issue
https://github.com/rogozinds/testbenchexample
What version of Testbench you are using?
Can you try to run it without using ChromeDriverManager, just download
chromedriver https://sites.google.com/a/chromium.org/chromedriver/downloads
and add it to your system PATH.
getDriver().get(URL) - should at least open a new Chrome Window and navigate to URL. But as I understood that's not happening?
P.S I've tried your example with Testbench 5 without ChromeDriverManager and simple index.html file it works.
Related
I'm trying to use Selenide and bonigarcia together with using multiple web driver, such as Chrome, Mozilla, Edge and etc.
This is what I've done:
public static Selenide driver;
public static void runBrowser(String browserName, String url) throws Exception {
if(browserName.equals("Chrome")) {
WebDriverManager.chromedriver().browserVersion(browserConfiguration.chromeVersion).setup();
Configuration.startMaximized = true;
driver.open(url);
}
else if(browserName.equals("Firefox")) {
WebDriverManager.firefoxdriver().browserVersion(browserConfiguration.firefoxVersion).setup();
Selenide.open(url);
}
else if(browserName.equals("Edge")) {
WebDriverManager.edgedriver().browserVersion(browserConfiguration.edgeVersion).setup();
driver.open(url);
} else {
throw new Exception("Something went wrong opening browser");
}
}
But, when I am trying to call that method with the parameter of "Firefox" or "Edge", it always runs on chrome. So, every time when I call that method, chromes web driver is running.
I've made it with Selenium, the difference between them is that instead of Selenide.open(url), I use WebDriver.get(url) and it works fine when I call a method with "Firefox" or "Edge" parameter.
Any ideas?
**EDIT: **
I added Configuration.browser = FirefoxDriverFactory.class.getName(); and now it looks like that: WebDriverManager.firefoxdriver().browserVersion(browserConfiguration.firefoxVersion).setup(); Configuration.browser = FirefoxDriverFactory.class.getName(); Selenide.open(url);
It will open any browser which I want, Chrome, Mozilla, Edge, etc.
But, somehow, I don't think that this is a real solution. I don't even know what have I done with adding FirefoxDriverFactory.class.getName() and why it is working now.
Selenide already includes WebDriverManager from bonigarcia.
You can just choose the browser either with
Configuration.browser = "firefox";
or setting SystemProperty selenide.browser, e.g. -Dselenide.browser=firefox
You don't need any Factories, WebDriverManager calls etc.
I created a custom webdriver (for a custom browser) by extending RemoteWebDriver. I can easily use it as standalone, by simply instantiating the driver.
But actually I want to use it in a Selenium Grid. Is there a way to register this custom web driver on a node, so that I can use it with via RemoteWebDriver and desired capabilities? I so, what do I need to do.
Any hint is welcome. Thanks in advance.
The WebDriver (server) variants are specifically designed/created/modified continously to be able to drive the ever evolving Web Browsers.
So if you want to drive a Custom Browser through a Custom Webdriver, it seems to be the perfect approach.
At this point, it is not clear from the question if your usecase resembles as a case where you don't actually want a browser.
However, as per the configuration in Browser.java the following set of Browsers are extensively tested before any release:
package org.openqa.selenium.testing.drivers;
import java.util.logging.Logger;
public enum Browser {
chrome,
edge,
ff,
htmlunit,
ie,
none, // For those cases where you don't actually want a browser
opera,
operablink,
safari;
private static final Logger log = Logger.getLogger(Browser.class.getName());
public static Browser detect() {
String browserName = System.getProperty("selenium.browser");
if (browserName == null) {
log.info("No browser detected, returning null");
return null;
}
try {
return Browser.valueOf(browserName);
} catch (IllegalArgumentException e) {
log.severe("Cannot locate matching browser for: " + browserName);
return null;
}
}
}
Solution
To make a provision for your own Custom Webdriver and Custom Browser you may need to add the relevant entries within Browser.java and other required files and you will be good to go.
Please follow the below set of instructions to setup support for a custom browser type when running selenium tests against a Selenium Grid.
Implement the interface org.openqa.selenium.remote.server.DriverProvider (or) extend org.openqa.selenium.remote.server.DefaultDriverProvider wherein you take care of building support for your custom browser.
Create a directory named META-INF\services under your main resources folder and ensure that this directory gets bundled into the jar, when you create a jar out of your project.
Create a service loader file named org.openqa.selenium.remote.server.DriverProvider wherein you add the fully qualified class name of the new class you created in step (1) and place it in the directory created in step (2)
Bundle your project into a jar.
Now start the selenium node by adding the jar created in (4).
Now your new browser is ready to be supported by the Selenium Grid.
Please refer to this selenium-users google forums thread which also talks about the same query wherein the user confirmed that the above mentioned approach worked for them.
You still need to take care of creating a new custom capabilities object from your client side when you instantiate the RemoteWebDriver object for your custom browser.
I am using Selenium and Java to write a test for Chrome browser. My problem is that somewhere in my test, I download something and it covers a web element. I need to close that download bar (I cannot scroll to the element). I searched a lot and narrowed down to this way to open the download page in a new tab:
((JavascriptExecutor) driver).executeScript("window.open('chrome://downloads/');");
It opens that new tab, but does not go to download page.
I also added this one:
driver.switchTo().window(tabs2.get(1));
driver.get("chrome://downloads/");
but it didn't work either.
I tried:
driver.findElement(By.cssSelector("Body")).sendKeys(Keys.CONTROL + "t");
and
action.sendKeys(Keys.CONTROL+ "j").build().perform();
action.keyUp(Keys.CONTROL).build().perform();
Thread.sleep(500);
but neither one even opened the tab.
It is because you can't open local resources programmatically.
Chrome raises an error:
Not allowed to load local resource: chrome://downloads/
Working solution is to run Chrome with following flags:
--disable-web-security --user-data-dir="C:\chrome_insecure"
But this trick doesn't work with Selenium Chrome Driver(I don't know actually why, a tried to remove all args that appears in chrome://version, but this doesn't helps).
So for me above solution is the only one that works:
C# example:
driver.Navigate().GoToUrl("chrome://downloads/")
There is another trick if you need to open downloaded file:
JavaScript example:
document.getElementsByTagName("downloads-manager")[0].shadowRoot.children["downloads-list"]._physicalItems[0].content.querySelectorAll("#file-link")[0].click()
Chrome uses Polymer and Shadow DOM so there is no easy way to query #file-link item.
Also you need to execute .click() method with JavaScript programatically because there is a custom event handler on it, which opens actual downloaded file instead of href attribute which point to url where you downloaded file from.
I started with this post and ended up with the solution given below. This one works in Chrome 71. First I highlighted the control and then clicked it.
The window object is actually the IWebDriver, the second method is called after the first one.
internal void NavigateToDownloads()
{
window.Navigate().GoToUrl("chrome://downloads/");
}
internal void OpenFirstDownloadLinkJS()
{
IJavaScriptExecutor js = (IJavaScriptExecutor) window;
js.ExecuteScript("document.getElementsByTagName('downloads-manager')[0].shadowRoot.children[4].children[0].children[1].shadowRoot.querySelectorAll('#content')[0].querySelector('#details > #title-area > #file-link').setAttribute('style', 'background: yellow;border: 2px solid red;');");
js.ExecuteScript("document.getElementsByTagName('downloads-manager')[0].shadowRoot.children[4].children[0].children[1].shadowRoot.querySelectorAll('#content')[0].querySelector('#details > #title-area > #file-link').click();");
}
Use this code (I wrote it in Python, but it should work in Java too with very slight modifications):
#switching to new window
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])
#opening downloads
driver.get('chrome://downloads/')
#closing downloads:
driver.close()
I have set the following properties:-
Set environment variables JAVA_HOME,path,ANT_HOME,path (using JDK version --> jdk1.7.0_40)
In Eclipse set the system Library, added all needed jars and select all jars in Order and Export.
Install the Firefox Version 24 and used the Selenium jar(selenium-server-standalone-2.41.0).
Giving no error while running the Java application, also no exception while not able to click on particular element.
sendKeys(Keys.ENTER) is working fine, but instead I need to use click() method.
Also code is working fine on other systems, I think I am missing something.
click() method is not giving any valid response, it makes me feel like its only declared not defined. I have tried on different websites, on different elements and with different types(xpath, css selector, name, id) but nothing worked for me. I took focus on that element by webdriver and also by highlighting the element using javascript code, its showing that particular element is present. I have performed other operations like entering username after clicking on that element, its performing well. I have worked with different browsers, different webpages also maximize the window using selenium but no positive response.
Any help will appreciate.
Thanks in advance
Hi the snippet below worked fine for me
public static void main(String[]args){
WebDriver appDriver;
appDriver = new FirefoxDriver();
appDriver.get("https://web210.qa.drfirst.com/login.jsp");
WebElement loginButton = appDriver.findElement(By.className("btn"));
loginButton.click();
appDriver.switchTo().alert().accept();
appDriver.close();
}
I was trying to click a button on my mobile web app, using selenium web driver. The button is located, the text over the button can be derived and even the click event is performing well. But the navigation doesn't occur.
I tried with Click() method, sendKeys() method and also with script executor. But couldn't process further on.
CODE:
public class TestWeb
{
WebDriver driver;
private Selenium selenium;
#Before
public void setUp() throws Exception {
driver = new IPhoneDriver();
driver.get("http://10.5.95.25/mobilebanking");
}
#Test
public void TC() throws Exception {
System.out.println("page 1");
Thread.sleep(5000);
WebElement editbtn1 = driver.findElement(By.id("ext-comp-1018"));
String s1 = editbtn1.getText();
System.out.println(s1);
editbtn1.click();
editbtn1.sendKeys(Keys.ENTER);
((JavascriptExecutor)driver).executeScript("arguments[0].click;", editbtn1);
System.out.println("ok");
}
#After
public void tearDown() throws Exception {
System.out.println("*******Execution Over***********");
}
}
I tried click, sendKeys and ScriptExecutor separately and also combined. It is executing without any error but the navigation doesn't occur.
Does anybody can help me with some other ways to perform click function on the button?
Ram
This may not be your issue but I noticed "ext-comp-" and guess you are using extjs.
I'm using GXT and while finding by id worked for many things, on some submit buttons it didn't.
I had to use firebug in firefox to locate the element and copy the xpath.
Then I could click the element by
driver.findElement(By.xpath("//div[#id='LOGIN_SUBMIT']/div/table/tbody/tr[2]/td[2]/div/div/table/tbody/tr/td/div")).click(); // worked
It was failing silently for me too. My submit button has the id of LOGIN_SUBMIT so I don't know why the following failed but ....
driver.findElement(By.id("LOGIN_SUBMIT")).click();//failed
Edit:
Here is an exact example (case 1 of 2):
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='gwt-debug-LOGIN_SUBMIT']")));
//wait.until(ExpectedConditions.elementToBeClickable((By.id("gwt-debug-LOGIN_SUBMIT")))); <!-- id works as well
OK so the element is found. It will timeout and throw an exception if it is not.
Still, the following fails (under firefox, works with chrome) with no error and the page does not navigate.
driver.findElement(By.xpath("//div[#id='gwt-debug-LOGIN_SUBMIT']")).click();
//driver.findElement(By.id("gwt-debug-LOGIN_SUBMIT")).click(); <-- fails too
What I have to do is:
driver.findElement(By.xpath("//div[#id='gwt-debug-LOGIN_SUBMIT']/div/table/tbody/tr[2]/td[2]/div/div/table/tbody/tr/td/div")).click();
So my experience was that even if I found the element with xpath, clicking failed unless I used a complete xpath.
Here is another exact example (case 2 of 2):
I can find an element like so:
WebElement we = driver.findElement(By.xpath("//*[#id=\"text" + i + "\"]"));
I know I have found it because I can see the text via:
we.getText();
Still selecting by the path I found it fails.
//get outta town man the following fails
driver.findElement(By.xpath("//*[#id=\"text" + i + "\"]")).click();
In this case there is not more explicit xpath to try as in case 1
What I had to do was use css:
//bingo baby works fine
driver.findElement(By.cssSelector("div#text" + i + ".myChoices")).click();
Actually, I obtained the css path via firebug than shortened it.
//this is what I recieved
html.ext-strict body.ext-gecko div#x-auto-0.x-component div#x-auto-1.x-component div#x-auto-3..myBlank div#choicePanel1.myBlank div.x-box-inner div#text3.myChoices //text3 is the id of the element I wanted to select
Whether or not you can figure out your needed xpaths and css selectors, I don't know, but I believe I experienced exactly what you did.