Web UI methods don't work correctly when I use Selenium web driver in Katalon Studio - selenium

Good Morning,
I'm using Katalon studio to automate my testing, and I need to launch the chrome driver using selenium, then use it to call Web UI methods in the test cases. the driver launched and opened successfully, but the Web UI methods such as (click, send keys…) in the test cases marked as passed while actually they didn’t, therefore the next step can’t be executed.
it’s noteworthy that the same method sometimes works and sometimes doesn’t.
My Code
def public downloadDocumentSetup() {
String OldDownloadsPath=RunConfiguration. getProjectDir () + "/Downloads"
String DownloadsPath= OldDownloadsPath.replace("/", "\\")
HashMap<Object, String> chromePrefs = new HashMap<Object, String>();
chromePrefs.put("download.default_directory", DownloadsPath)
chromePrefs.put("profile.default_content_settings.popups",0)
chromePrefs.put("download.prompt_for_download", false)
chromePrefs.put("safebrowsing.enabled", "true");
ChromeOptions options = new ChromeOptions()
options.setExperimentalOption("prefs", chromePrefs)
DesiredCapabilities cap = DesiredCapabilities.chrome()
cap.setCapability(ChromeOptions.CAPABILITY, options)
System.setProperty("webdriver.chrome.driver", DriverFactory.getChromeDriverPath())
WebDriver driver = new ChromeDriver(cap)
DriverFactory.changeWebDriver(driver)
RunConfiguration. setWebDriverPreferencesProperty ("prefs", chromePrefs)
}
My Test Case
WebUI.navigateToUrl(GlobalVariable.WWWSite)
WebUI.click(findTestObject(‘Pages/MyFirstObject’))
WebUI.click(findTestObject(‘Pages/MySecondObject’))
Result
The browser opened and navigated to the website successfully, and the first click step marked as passed but actually it didn’t pass, therefore the next click action is not working.
Note
My Test case works perfectly when I use ‘Open Browser’ method to launch the driver.

Related

Accept microphone and camera permissions on Edge using selenium

I am running selenium scripts on Edge browser. one of the functionality requires to initiate a audio or video call between two windows. In chrome, we can use 'use-fake-ui-for-media-stream' in chrome options. Is there anything similar for Edge. If there isn't, is there a way to accept these alerts at runtime. I have tried -
driver.switchTo().alert().accept(),
but this also doesn't work, and throws error saying no such alert present
Edited
I am using Edge chromium and java selenium and have set properties as below in code. Still permission pop up shows when script runs
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);
prefs.put("download.default_directory", fileDownloadLocation);
EdgeOptions options= new EdgeOptions();
options.setCapability("prefs", prefs);
options.setCapability("allow-file-access-from-files", true);
options.setCapability("use-fake-device-for-media-stream", true);
options.setCapability("use-fake-ui-for-media-stream", true);
DesiredCapabilities capabilities = DesiredCapabilities.edge;
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
capabilities.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS,true);
System.setProperty("webdriver.edge.driver", getDriverPath("EDGE"));
driver = new EdgeDriver(options);
driver.manage().window().maximize();
I suggest you make a test with the sample code below. I tried to test with the Edge Chromium browser and it looks like it is not asking the permission popup.
JAVA code:
public static void main(String[] args)
{
System.setProperty("webdriver.edge.driver","\\msedgedriver.exe");
EdgeOptions op=new EdgeOptions();
op.addArguments("use-fake-device-for-media-stream");
op.addArguments("use-fake-ui-for-media-stream");
WebDriver browser = new EdgeDriver(op);
browser.get("https://your_URL_here...");
}
In Selenium 3.141 Version we dont have addArguments() method but in Selenium 4.0.0 alpha version we have addArguments() method
EdgeOptions edgeOpts = new EdgeOptions();
edgeOpts.addArguments("allow-file-access-from-files");
edgeOpts.addArguments("use-fake-device-for-media-stream");
edgeOpts.addArguments("use-fake-ui-for-media-stream");
edgeOpts.addArguments("--disable-features=EnableEphemeralFlashPermission");
driver = new EdgeDriver(edgeOpts);

Pre-existing add-ons do not work after starting from a Selenium custom profile

I have a code in C # with very simple Selenium. It is just controlling a VPN extension and navigating to X Page.
The question is that after Selenium starts with the Firefox Custom Profile, the Extensions that the Profile has installed disappear, so it does not allow me to control VPN Extensions.
On My Machine Works Correctly The Problem Is When Executing It On Another Machine.
FirefoxProfile profile1 = new FirefoxProfile(#"C:\Users\Familia\AppData\Roaming\Mozilla\Firefox\Profiles\6r8313wg.1");
FirefoxOptions options1 = new FirefoxOptions();
options1.Profile = profile1;
FirefoxDriver driver1 = new FirefoxDriver(options1);
driver1.Manage().Window.Maximize();
do
{
driver1.Navigate().GoToUrl("moz-extension://3667138c-f19d-49a2-8ffe-278b50c1e52c//popup.html");
Thread.Sleep(10000);
IWebElement VPN15 = driver1.FindElement(By.ClassName("server-item__server"));
VPN15.Click();
Thread.Sleep(5000);
driver1.Navigate().GoToUrl("https://www.google.com/");
Thread.Sleep(70000);
} while (!i.Checked);
driver1.Quit();

IE browser gets shrunk during execution

I am facing an weird issue. I have a selenium suite to execute on multiple browsers. It works fine of chrome and Firefox. But in case of IE, the web page under test gets shrunk (web page resize) making elements hidden. Hence facing NoSuchElementException.
I have already tried executing on full screen. No help.
Please help in solving this issue.
Thanks,
Praveen
Don't know the exact reason for that, however, suggested workaround would be to implement pre-execution method for your test which would take care of all your browser different configurations. For example, if you are using Java with Selenium and JUnit5, it might be:
#BeforeEach
void beforeTestExecution() {
DesiredCapabilities desiredCapabilities = DesiredCapabilities.internetExplorer();
desiredCapabilities.setCapability(
CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
driver = new InternetExplorerDriver(desiredCapabilities);
// Navigate to URL
driver.get("http://www.yoursite.com");
// Maximize your website window before test.
driver.manage().window().maximize();
}
Or:
#BeforeEach
void configBrowser() {
DesiredCapabilities desiredCapabilities = DesiredCapabilities.internetExplorer();
desiredCapabilities.setCapability(
CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
driver = new InternetExplorerDriver(desiredCapabilities);
// Start Internet Explorer maximized.
driver.manage().window().maximize();
}

Cross browser test

I have to do login to an application with two different browsers (IE & FF) and hence I tried to do cross browser test. When the URL gets passed to IE, I am getting link as "Continue to this website not recommended", whereas the same link will not be displayed in FF. In the below if I use this statement driver.findElement(By.id("overridelink")).click(); then it works fine in IE but failing in firefox since there wont be any link in FF.
please let me know the possible where one script should run in both the browsers. Below is the script which am trying
#Test
#Parameters("browser")
public void verifypagetitle(String browsername) {
if(browsername.equalsIgnoreCase("IE"))
{
System.setProperty("webdriver.ie.driver",
"D:\\2.53.1\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
else if (browsername.equalsIgnoreCase("firefox"))
{
System.setProperty("webdriver.firefox.bin",
"C:\\Program Files\\Mozilla Firefox\\firefox.exe");
driver = new FirefoxDriver();
}
driver.get("URL");
driver.manage().window().maximize();
driver.findElement(By.id("overridelink")).click();
driver.findElement(By.id("j_username")).sendKeys("userid");
driver.findElement(By.id("j_password")).sendKeys("pwd");
driver.findElement(By.id("j_password")).submit();
This is most likely caused by the IE security level. In my experience if I change it to Low, the warning is not present. You can try and see if this will solve the issue in your case.
Another solution is to add conditional logic to check the browser being used. Something like this:
Capabilities cap = ((RemoteWebDriver) browserDriver).getCapabilities();
String browsername = cap.getBrowserName();
// This block to find out if it is IE
if ("internet explorer".equalsIgnoreCase(browsername)) {
driver.findElement(By.id("overridelink")).click();
}

How to run selenium code

I have the code written for selenium, but I do not know how to make selenium read the code. I know that you have to put it in a certain folder somewhere though. I have never used this program or anything like it before. I just need to know how to make selenium read my code. I have no idea what I'm doing here.
public class GoogleTest {
WebDriver driver = new ChromeDriver();
String baseUrl = "google.com";;
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
selenium.open("google.com");
selenium.type("name=q", "cheese");
selenium.click("name=btnG");
WebDriver driverInstance = ((WebDriverBackedSelenium) selenium).getUnderlyingWebDriver();
}
You need to write the following line before creating the driver object:
System.setProperty("webdriver.chrome.driver", "C:/path/to/chrome/driver/chrome.exe");
Try following code in same order:
System.setProperty("webdriver.chrome.driver", "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe");
WebDriver driver = new ChromeDriver();
String baseUrl = "google.com";;
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
.
.
You may need to download the chrome driver as well.
link: https://code.google.com/p/selenium/wiki/ChromeDriver