Selenium WebDriver Automation for Vaadin components - java

I am using Selenium Java WebDriver to automate a Vaadin based web application. When I navigate to a page and click a button it opens up another small window which does not look like a popup to me. It seems the subwindow is dynamically added to the main html.
Does anyone have any idea on how to identify Vaadin elements in Selenium?

try with
driver.findElement(By.className("v-window")) for the entire window or
driver.findElement(By.className("v-window-contents")) for the window content.

When you creating test be sure to run this test after the browser is open. Sometimes test starts, but browser is not open. I put Thread.sleep(1000); in #Before class, to be sure chrome is open.
#Before
public void openBrowser() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "chromedriver");
driver = new ChromeDriver();
driver.get("http://localhost:8000");
Thread.sleep(1000);
}
and then
#Test
public void testLoginFormExists() {
WebElement login = driver.findElement(By.id("username"));
WebElement password = driver.findElement(By.id("password"));
...
}
or even better, check this answer: https://stackoverflow.com/a/9430377/1168786

Related

Selenium: How to paste URL onto a new tab - Java

I am struggling to figure out a way to paste the text/URL onto a new tab in Chrome. I am running my tests on Linux in headless mode.
So, I was able to launch a new tab but unable to paste the URL on the navigation bar.
Why I am pasting URL? I have to click a button which will give me the URL, and I have to launch the URL on a new tab.
Here's the code I am trying to make it work.
copyBtn.click(); //copying the URL
((JavascriptExecutor) driver).executeScript("window.open()");// launching a new tab
SeleniumUtils.switchBrowserTab(driver, 1);
Actions actions = new Actions(driver);
actions.sendKeys(Keys.COMMAND, "v").sendKeys(Keys.ENTER).build().perform(); //sending the paste command
System.out.println(driver.getCurrentUrl());
The sysout is printing about:blank instead of the pasted text. I think its because I am not focussing on the navigation bar. I ready many blogs but seems like there is no way to focus on the navigation bar.
Could someone please throw some light on how can this use-case be achieved?
Also, I don't have the URL handy, to be honest i am not sure how can i get the copied URL. Because I am not running locally, i cannot use ToolKit.
Thank you.
This I used a lot:
public void openNewTab(WebDriver driver) throws InterruptedException {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.open('');");
Thread.sleep(100);
}
public void gotoTab(WebDriver driver, int tabIndex) throws InterruptedException {
List<String> winHandles = new ArrayList<>(driver.getWindowHandles());
Thread.sleep(500);
driver.switchTo().window(winHandles.get(tabIndex));
}
WebDriver driver = ...;
openNewTab(driver);
gotoTab(driver, 1); // zero based
driver.get("...");

WebDriver isn't launching the ChromeDriver

I am developing a test case using selenium to verify that a user has reached the properties list on a travel site. When i go to run the application on eclipse, the driver is failing.
I have tried using the "WebElement" method as another option but, that is giving me the same issue.
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Extracted Files\\Chrome driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.booking.com/");
driver.manage().window().maximize();
driver.findElement(By.className("input-location.location-field.px-2")).sendKeys("Krakow, Poland");
Thread.sleep(2000);
driver.findElement(By.className("btn btn-primary btn-block mt-4 submit-search-form")).click();
Thread.sleep(2000);
driver.close();
}
Expected result is to bring up all the listing for the searched area on the properties page. Actual result is the application is simply failing to execute and is highlighting the "WebDriver driver = new ChromeDriver" line.
Any help or assistance/documentation would be greatly appreciated.

Unable to locate the elements of SmartGWT application using Selenium in IE browser

I'm testing a GWT + SMARTGWT application like Paint and I'm trying to locate the elements of this web application using Selenium Webdriver. The method which I have used to locate the elements is by the relative XPath of those elements but the problem which I am currently facing is that this method is working correctly on the browsers like Chrome, Firefox, and Edge but not on the IE browser. The version of IE on my PC is 11.1593.14393.0. In the IE browser, this relative XPath method is giving a TimeOutException. I have given the explicit wait:
wait.until(ExpectedConditions.elementToBeClickable(webelement));
The IE browser is not able to find the element. I am also getting the following exception sometimes for other elements:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: Unable to locate an element with the xpath expression //img[contains(#src,'Insert XXX'] because of the following error:
Error: Bad token: ]
Among the troubleshooting solutions to this issue, I tried enabling/disabling the protected mode in IE for all the levels but this method didn't work. Along with that, I also tried checking the box next to the option - "Allow active content to run files on My Computer" but this method also failed to work.
What should I do to fix my issue?
This is my code. Here firstly, I will click on the Insert button located on the top bar of the application and after clicking on the Insert button, a window will launch on which I will click on the Close button.
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.ie.driver", "D:\\SELENIUM\\Drivers\\iedriverserver.exe");
WebDriver driver = new InternetExplorerDriver();
Thread.sleep(3000);
driver.get(baseURL);
WebDriverWait wait = new WebDriverWait(driver, 10);
final String InsertPath = "//img[contains(#src,'Insert XXXX')]";
final String closePath="//img[contains(#src,'close')]";
WebElement Insert = driver.findElement(By.xpath(InsertPath));
wait.until(ExpectedConditions.elementToBeClickable(Insert));
Thread.sleep(2000);
Insert.click();
WebElement close = driver.findElement(By.xpath(closePath));
wait.until(ExpectedConditions.elementToBeClickable(close));
Thread.sleep(3000);
close.click();
}
}
Edit: I also used finding the element using Javascript executor in my code as follows:
WebElement Insert = driver.findElement(By.xpath(InsertPath));
Thread.sleep(2000);
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].click();", Insert);
Sadly, this method also failed to work in the IE browser.
So, I was able to locate the elements by using the latest driver of the Internet Explorer and giving the following desired capabilities in my code to the IE browser.
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability("requireWindowFocus", true);
ieCapabilities.setCapability("unexpectedAlertBehaviour", "accept");
ieCapabilities.setCapability("ignoreProtectedModeSettings", true);
ieCapabilities.setCapability("disable-popup-blocking", true);
ieCapabilities.setCapability("enablePersistentHover", true);*/
System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver(ieCapabilities);

selenium open net tab in a existing windows

I have open a browser windows with something page and when I run the code :
public class First {
public static void main(String[] args) throws InterruptedException {
String exePath = "C:\\Users\\Arsed\\Desktop\\MicrosoftWebDriver.exe";
System.setProperty("webdriver.edge.driver", exePath);
WebDriver driver=new EdgeDriver();
driver.get("http://www.facebook.com");
it open a new windows with "facebook" not a new tab in the existing windows
This is Selenium's default behaviour.
At least it opened a window.
You will not intercept an already-opened browser's window (for example the one you read my answer in now) with Selenium WebDriver.
You could do it perhaps with Robot class in Java or with AutoIt.

Automating switching browsers when running Selenium-Java tests

I am currently working on a project using Java, Selenium and Testng. My overall goal is to test the functionality of a webpage over different web browsers. I have my Selenium code working and am able to run the test on Chrome and Firefox. However, I have to manually change the code to switch the browser. I do this by commenting out driver = new ChromeDriver();
I would like to edit my code so the test runs in Firefox and when that test is complete start the test in Chrome. Can someone please guide me in the right direction?
Here is a sample of what my code looks like:
WebDriver driver = null;
Selenium selenium = null;
#BeforeSuite
public void setup() throws Exception {
/// Chrome Driver ///
System.setProperty("webdriver.chrome.driver", "mac/chromedriver.exe");
//driver = new ChromeDriver();
/// Firefox Driver ///
driver = new FirefoxDriver();
}
#Test
public void testGoogle() throws Exception {
selenium = new WebDriverBackedSelenium(driver,"URL");
There could be quite a few ways of achieving this.
In the setup you can read a property and based on that you can instantiate the right driver.
String driverType = System.getProperty("driverType");
if ("firefox".equals(driverType))
driver = new FirefoxDriver().....
You can run the test twice, once with firefox property and then with chrome property.
Other option is to keep all the test in one class. Then extend this class by two classes, one for firefox setup and another for chrome setup. Then you can run both the subclass tests in a suite. They would run one after the another.

Categories

Resources