Mobile emulator Selenium tests fail in Jenkins but not in cmd - java

I have a test that always fail when running inside Jenkins.
My project includes Selenium webdriver, JAVA, Maven, TestNG, Jenkins, Allure (reports).
I have a few suites of tests with 100+ test cases, and I iterate them through 3 different browsers (the tests run in parallel using TestNG). They all run (using maven command line) and pass in my development laptop, and on the test server when using a command line.
I have 2 problems regarding Jenkins and separated them into 2 questions- one of them is described in this question, and the other (IE11 issue) is here.
The problem starts when running inside Jenkins in the test server!
The test fail in mobile emulator (Chrome browser) - in the test I click on a link to verify that a new window was opened with the correct url.
I tried 3 types of clicks (Selenium click, Actions, JS) and all returned a null handle.
The code:
Here I create the main window handle and click the link:
String mwh = driver.getWindowHandle();
WebElement poweredBy = (new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(Consts.POWERED_BY_XPATH_1000))));
poweredBy.click();
And this is, part of, the method that gets the handle and verify the new window:
public boolean closePopupWindow(String mwh, String mTitle, String layoutNumber) {
// For IE11- make sure popup blocker is turned off in the options. else it will have only one window handle and fail
boolean isOpenedWindowCorrect = false;
String newWindow = null;
Set<String> handlers = driver.getWindowHandles();
for (String window : handlers) {
if (!window.equals(mwh)) {
newWindow = window;
}
}
// the focus is on the main page. need to switchTo new page and close it
driver.switchTo().window(newWindow);
System.out.println("The focus now is on the NEW window");
String newTitle = driver.getTitle();
System.out.println(newTitle);
This is the error I'm getting:
java.lang.NullPointerException: null value in entry: handle=null
at com.google.common.collect.CollectPreconditions.checkEntryNotNull(CollectPreconditions.java:34)
at com.google.common.collect.SingletonImmutableBiMap.(SingletonImmutableBiMap.java:42)
at com.google.common.collect.ImmutableBiMap.of(ImmutableBiMap.java:73)
at com.google.common.collect.ImmutableMap.of(ImmutableMap.java:123)
at org.openqa.selenium.remote.RemoteWebDriver$RemoteTargetLocator.window(RemoteWebDriver.java:995)
at il.carambola.pages.Page.closePopupWindow(Page.java:786)
Do you think there is a security issue that Jenkins wont open new windows in the browser? Is it VERY slow to open the window?
The same tests PASS when not using mobile emulator. (I have the same test in Chrome and Firefox and it succeed to click and pass the verification).
JDK 1.8.0_162
Jenkins V 2.121.1
Server- AWS t2.large - 8GB RAM, Windows server 2016 Data center, 64bit

It's clear that when your program gets to this line
driver.switchTo().window(newWindow);
that newWindow is still null. The way this is written, it could be a timing issue, and it could be another issue causing the popup to not show up. To make sure it's not a timing issue, I would suggest adding some kind of wait for there to be multiple window handles before you try to switch windows. Something like
(new WebDriverWait(driver,10)).until(d -> d.getWindowHandles().size() == 2);
If that wait fails, then you know the popup is being blocked and can go from there.

Related

Maximize browser window in LeanFT

I am looking for some solution, like this in Selenium WebDriver:
ChromeOptions options = new ChromeOptions();options.addArgument("--start-maximized");
So browser window should be maximized when test is executed.
I found a profile based solution for this problem, but it opens a lot of tabs, which is maybe caused by escape characters.
#Test
public void chromeWithProfileLaunch() throws Exception {
String profileDir = "--user-data-dir=\"c:\\Temp\\profile1\""; //should be different folder every time
String leanftChromeExtension = "--load-extension=C:\\Program Files (x86)\\HPE\\LeanFT\\Installations\\Chrome\\Extension"; //to load the LeanFT extension
String homePage = "www.google.com"; //the homepage to start with
new ProcessBuilder("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", profileDir, leanftChromeExtension, homePage)
.start();
Thread.sleep(2000); //wait for Chrome process to load
Browser openedBrowser = BrowserFactory.attach(new BrowserDescription.Builder().title("Google").type(BrowserType.CHROME).build());
Verify.areEqual(homePage, openedBrowser.getURL());
}
I don't know about maximized but LeanFT supports putting the browser in fullScreen mode.
LeanFT doesn't support maximize() out of the box yet.
However, you could use sendKeys() method.
I'm not entirely sure if you can to it on the browser instance directly, or you need to getPage() first, but you can definitelly send Super key (win key) + ↑ as specified here. ↓ for restoring back to the initial state.
Here's an example using sendKeys with Java SDK if you need it.

Switching between tabs using webdriver in chrome

Am Opening a page performing some actions on that and i am using this piece of code to open another link in the next tab
String url = "https://qa.logfireapps.com/lgf_700_qa_rf";
String args1 = String.format("window.open('%s', '%s');", url, "new");
((JavascriptExecutor) driver).executeScript(args1);
Then i need to switch between this two tabs.
I used driver.switchTo.window(parentWin);
I also used this code
List windowHandles1 = new ArrayList(driver.getWindowHandles());
driver.switchTo().window(windowHandles1.get(1));
Both of the cases did not work for me,but still the web driver code is running successfully without any errors even its not switching to first window.
I need to switch to the first tab,but all the actions are going on in the first tab but still in UI am seeing the second tab opened,It is not switching to the first tab but my whole webdriver code gets passed.
It is observed that switching problem happens only with this two sites
1) https://qa.logfireapps.com/lgf_700_qa/index/
2) https://qa.logfireapps.com/lgf_700_qa_rf
This was my solution to this using python. sorry no java example
def switch_to_new_window(driver, window):
driver.switch_to_window(driver.window_handles[window])

Switching windows in Firefox 34 hangs

I am using Selenium webdriver 2.44 with firefox 34 on windows 7 machine. I have a script where I hover over an 'open page' icon and click it. The click opens the a new tab manually and in chrome driver 2.15. The scenario opens a new window instead of tab which I handle in firefox-34. below is the code.
public void switchWindow(){
//try {
String winHandleBefore = driver.getWindowHandle();
System.out.println("before "+winHandleBefore);
Set<String> windows = driver.getWindowHandles();
for (String window : windows) {
driver.switchTo().window(window);
if (driver.getTitle().contains(winHandleBefore)) {
return;
}
}
The problem I am facing is one or two tests run for more than 10,000 seconds as they look like they hang at switching the windows from parent to child. This is issue is reproducible each and every time.Has anybody seen this issue?. Is there a workaround?.Kindly let me know if additional information needs to be provided.
I have fixed this issue by closing the first window and shifting the focus back on the newly opened window. I didn't expect the fix would be this simple.
The fix works for FF-35 as well.

How to handle overlay/popup window using selenium webdriver

I am trying to automate initial configuration(of my server) through webpage. After hitting my server ip https:/localhost:4443 and entering my credentials ,i get a window to change password(overlay/popup window).
Problem:- if i browse the same ip from another m/c or from another browser , i get a window over window i.e one more window over change password window(Please click the link to see the screenshot).
What i tried is to get the handle of the window but its not working, its providing one handle only.
**Its not frame also.
HTML code -- https://dl.dropboxusercontent.com/u/91420517/Html_Code.JPG
Here's my code
WebDriver driver=new FirefoxDriver();
driver.get("https://localhost:4443/ControlPoint/");
driver.findElement(By.xpath("//*[#id='name']")).sendKeys("xxxxxx");
driver.findElement(By.xpath("//*[#id='pass']")).sendKeys("xxxxxx");
driver.findElement(By.xpath("//*[#id='loginForm123']/div[6]/div[1]/div")).click();
Set<String> winIds = driver.getWindowHandles();
System.out.println("Total Windows --- " + winIds.size()); // its resulting the size as 1 which is not correct.
Iterator<String> it = winIds.iterator();
String mainWin=it.next();
String changeWin=it.next();
String shareWin =it.next();
driver.switchTo().window(shareWin);
String warning = driver.findElement(By.xpath("html/body/div[4234]/div[1]/span")).getText(); // to get the text on 3 window
System.out.println(warning);
How to resolve this issue .Please help. Any other way to click on buttons on window 3.
If the additional window is opened asynchronously, then possibly you are checking for it (with getWindowHandles()) too early, before it has been created - this is a common issue with Selenium tests and asynchronous page updates.
If this is the issue, it can be solved by trying a few times with a wait in between, checking each time whether a new window has appeared.

Bring the Firefox Browser to Front using selenium Java (Mac OSX)

I am using three instances of fire fox driver for automation.I need to bring current active firefox browser into front, Because I am using some robo classes for some opertation. I had tried java script alert for google chrome in mac ( same operation) and its worked fine. In windows used user32 lib. In the case of firefox mac its showing the alert in background but the web page is not come into front.
((JavascriptExecutor)this.webDriver).executeScript("alert('Test')");
this.webDriver.switchTo().alert().accept();
The above code I used for chrome in Mac. Same code is working and showing alert for firefox but the window is not coming to front.
Please suggest if there any other method for doing the same in firefox.
Store the window handle first in a variable, and then use it to go back to the window later on.
//Store the current window handle
String currentWindowHandle = this.webDriver.getWindowHandle();
//run your javascript and alert code
((JavascriptExecutor)this.webDriver).executeScript("alert('Test')");
this.webDriver.switchTo().alert().accept();
//Switch back to to the window using the handle saved earlier
this.webDriver.switchTo().window(currentWindowHandle);
Additionally, you can try to maximise the window after switching to it, which should also activate it.
this.webDriver.manage().window().maximize();
Try switching using the window name:
driver.switchTo().window("windowName");
Alternatively, you can pass a "window handle" to the switchTo().window() method. Knowing this, it’s possible to iterate over every open window like so:
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
Based on the Selenium documentation: http://docs.seleniumhq.org/docs/03_webdriver.jsp
As described in other topics, you can use
driver.manage().window().setPosition(new Point(-2000, 0));
too.
# notifications for selenium
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
current_path = os.getcwd() # current working path
chrome_path = os.path.join(current_path, 'chromedriver')
browser = webdriver.Chrome(executable_path=chrome_path, chrome_options=chrome_options)
browser.switch_to.window(browser.current_window_handle)
browser.implicitly_wait(30)
browser.maximize_window()
browser.get("http://facebook.com")
Only thing that worked for me on mac: self.driver.fullscreen_window().

Categories