On using this class augmentedDriver, it's launching unnecessary browser Instances - selenium-webdriver

I am using following code snipped to take screenshot in selenium 2.21.
augmentedDriver = new Augmenter().augment(driver);
File scrnshot = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrnshot, new File(File_Name));
Whenever i call method containing this code, it's launching new browser instances with text "This is the initial start page for the WebDriver server.
"
driver = new new InternetExplorerDriver();
Please let me know the issue, as well as solution too.

I think, that this line:
augmentedDriver = new Augmenter().augment(driver);
Starts new Driver instance. But you need to give here:
File scrnshot = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
Not augmentedDriver but existing driver instance.

Related

Jmeter webdriver wait funtion - Can not create new object with constructor org.openqa.selenium.support.ui.WebDriverWait with the passed arguments

I am trying to use the JMeter Selenium Webdriver wait function but getting error output as -javax.script.ScriptException: TypeError: Can not create new object with constructor org.openqa.selenium.support.ui.WebDriverWait with the passed arguments; they do not match any of its method signatures. in at line number 2
var ui = JavaImporter(org.openqa.selenium.support.ui)
var wait = new ui.WebDriverWait(WDS.browser,120)
Code in the webdriver sampler:
var ui = JavaImporter(org.openqa.selenium.support.ui)
var wait = new ui.WebDriverWait(WDS.browser,120)
WDS.sampleResult.sampleStart()
WDS.browser.get('http://jmeter-plugins.org')
WDS.sampleResult.sampleEnd()
I am using the latest version of Jmeter (5.5) and the latest selenium webdriver support package (4.5.1). This use to work before. Can someone help here please? thanks!!
I have tried upgrading and degrading the Jmeter, but no luck.
WebDriver Sampler 4.5.1 comes bundled with selenium-support 4.5.0 and WebDriverWait constructor for this version expects 2nd argument to be Duration object
So you need to do something like:
var wait = new ui.WebDriverWait(WDS.browser, java.time.Duration.ofSeconds(120))
Also be informed that since JMeter 3.1 it's recommended to use Groovy language for scripting mainly for performance reasons so you might want consider switching, it would be way easier to debug your test.

Element is not clickable at point ... error when using headless browsing

I have this error "org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element is not clickable at point (209, 760)", when I run the piece of code below in headless mode. When it is run with browser displayed I have no error and test passes fine. As you can see below, I trie with waiting, js executor, actions move to element but still no good result. I am using xpath to locate / define the element, and not coordinates. Why is this happening please and how can I solve it? Thanks in advance.
#Test(priority = 1)
public void verifyAddUserWithMarkedMandatoryFields() {
// accessing add user webpage / functionality
userListObject.getAddUserButton().click();
// inserting data to complete form
addOrEditUserPageObject.insertCredentials(userModel.getUsername(), userModel.getEmail(), "", userModel.getPassword());
// clicking Submit when becoming enabled
WebDriverWait myWaitVariable = new WebDriverWait(driver, 5);
myWaitVariable.until(ExpectedConditions.elementToBeClickable(addOrEditUserPageObject.getSubmitButtonAddOrEdit()));
// Actions actions = new Actions(driver);
// actions.moveToElement(addOrEditUserPageObject.getSubmitButtonAddOrEdit()).click().perform();
JavascriptExecutor jse = (JavascriptExecutor)driver;
// jse.executeScript("scroll(209, 760)"); // if the element is on top.
jse.executeScript("scroll(760, 209)"); // if the element is on bottom.
addOrEditUserPageObject.getSubmitButtonAddOrEdit().click();
}
You should add screen size for the headless mode, something like this:
Map<String,String> prefs = new HashMap<>();
prefs.put("download.default_directory", downloadsPath); // Bypass default download directory in Chrome
prefs.put("safebrowsing.enabled", "false"); // Bypass warning message, keep file anyway (for .exe, .jar, etc.)
ChromeOptions opts = new ChromeOptions();
opts.setExperimentalOption("prefs", prefs);
opts.addArguments("--headless", "--disable-gpu", "--window-size=1920,1080","--ignore-certificate-errors","--no-sandbox", "--disable-dev-shm-usage");
driver = new ChromeDriver(opts);
I put much more things here, the only relevant point here is "--window-size=1920,1080", this should resolve your problem.
The rest is to show how things are managed, including other relevant settings for headless mode.

OpenQA.Selenium.WebDriverException: [windowHandle] is not a top level window handle solution

Some people including me were suffering from this issue called "OpenQA.Selenium.WebDriverException: [windowHandle] is not a top level window handle".
There are lot of question asked and answered on "how to attach to a TopLevelWindow" but literally none is talking on "how to attach to a NonTopLevelWindow". I searched a lot for a solution but there was nothing on it. But after reading a code shared in this answer on GitHub, I realized what the solution is.
I was so disgusted after it because of the simplicity of the solution! Then I thought to share it with everyone.
The solution is so simple. As the new window which is a Child Node (i.e. resides under the main application tree), we can not attach to it by creating a new session. The reason is that, it is not a top level window and we can only attach to a top level window with this process explained on GitHub.
What we need to do is just simply search for the window by its name and you will get access of that window (same as finding an UI element)
For example- This code is for such a window which is a TopLevelWindow:
# create a desktop session to find the new window
desired_caps = {}
desired_caps["app"] = "Root"
newDriver = webdriver.Remote(WindowsApplicationDriverUrl, desired_caps)
# Find the NativeWIndowHandle of the new window
newWindow = newDriver.find_element_by_name("SmarTTY - New SSH Connection")
newWindowHandle = newWindow.get_attribute("NativeWindowHandle")
# create a new session to attach the new window with its NativeWindowHandle
desired_caps = {}
desired_caps["appTopLevelWindow"] = newWindowHandle
connWinDriver = webdriver.Remote(WindowsApplicationDriverUrl, desired_caps)
And this code is for such a window which is a NonTopLevelWindow:
# Find the new window
newPopUpWindow = driverMain.find_element_by_name("SmarTTY - New SSH Connection")
In this case the first code will not work due to the window is not being a top level window. But the second code will work.
This window (window name is SmarTTY - New SSH Connection) pops open after I click a button on the previous window. The application name on which this example is based on is SmarTTY. And the above codes are Python codes for WinAppDriver.
Here is my C# code, I am adding it as desired capabilities are depricated and options should be used instead:
AppiumOptions rootSessionOptions = new AppiumOptions();
rootSessionOptions.AddAdditionalCapability("app", "Root");
rootSessionOptions.AddAdditionalCapability("deviceName", "WindowsPC");
_driver = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), rootSessionOptions);
_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
var VSWindow = _driver.FindElementByName("Your project name without .csproj - Microsoft Visual Studio");
var VSTopLevelWindowHandle = VSWindow.GetAttribute("NativeWindowHandle");
VSTopLevelWindowHandle = (int.Parse(VSTopLevelWindowHandle)).ToString("x");
AppiumOptions VisualStudioSessionOptions = new AppiumOptions();
VisualStudioSessionOptions.AddAdditionalCapability("appTopLevelWindow", VSTopLevelWindowHandle);
_driver = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), VisualStudioSessionOptions);
_driver.SwitchTo().Window(_driver.WindowHandles[0]);
# After login i am not able to perform any operation
#Conftest file code
import pytest
from appium import webdriver
#pytest.fixture(scope="class")
def setUp(request):
desired_caps = {}
desired_caps["app"] = r"C:\\Program Files (x86)\\Giesecke Devrient\\Compass VMS\\Compass VMS.exe"
driver = webdriver.Remote(command_executor='http://127.0.0.1:4723',
desired_capabilities= desired_caps)
#driver.implicitly_wait(10)
request.cls.driver = driver
yield
driver.close()
#Main Code
from appium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from Utility.Base import BaseClass
# #pytest.mark.usefixtures("setUp")
class TestDe_Login(BaseClass):
def test_initialize(self):
self.driver.find_element_by_xpath("//*[#Name='Login']//*[#LocalizedControlType='text' "
"and #Name='User ID:']/..//*[#LocalizedControlType='edit']").send_keys("t2")
self.driver.find_element_by_accessibility_id("passwordTextBox").send_keys("1")
self.driver.find_element_by_name ("&Login").click()
wait = WebDriverWait(self.newDriver, 20)
wait.until(EC.presence_of_element_located((By.NAME, '&Logout'))).click()[enter image description here][1]

How to take full page screen shot in internet explorer using selenium webdrivers

I am not able to take the whole page screen shot using selenium webdriver.I am using internet explorer.
I tried robot function of java using mouse roll button but failed.
Please try the following let me know if it works for you
WebDriver driver = new FirefoxDriver();
driver.get("http://www.somewebsite.com/");
File imgFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(imgFile , new File("c:\\tmp\\test.png"));
if i have misunderstood you que please let me know
If the above answer fails, you can give a try making using of Augemented WebDriver, which can be used with IEDriver on it's latest version, try following code
WebDriver augmentedDriver = new Augmenter().augment(driver);
File screenFile = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenFile.getAbsolutePath(), new File("c:\\tmp\\test.png"));
You can also get screenshot in the form of bytes, by switching the OutputType as required
.getScreenshotAs(OutputType.BYTES)

How do you create a browser profile for Selenium's PhantomJS/GhostDriver?

Here's how you create a Firefox profile:
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.dir", download_dir)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv")
How do you do it with PhantomJS (GhostDriver)?
The closest you can get with phantomjs is to use the driver capabilities:
DesiredCapabilities caps = DesiredCapabilities.phantomjs();
caps.setCapability( "phantomjs.page.settings.userAgent", "Mozilla");
Set<String> cliArgs = new HashSet<>();
cliArgs.add("--ignore-ssl-errors=true");
cliArgs.add("--ssl-protocol=any");
cliArgs.add("--web-security=false");
caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgs);
driver = new PhantomJSDriver(caps);
However, you notice that there are no configuration options for automatic downloading, since phantomjs does not support this. It is anyway not a very good idea to use selenium for testing of downloads. I did answer another related question earlier in which I point to an article about this and why you should not do it.

Resources