Run exe file using selenium webdriver - java

How can I run exe file using selenium webdriver. If i can run the exe file then i can automate the windows window using auto it tool and can run those exe using java selenium. it would help in browsing the file in Selenium

Yes, you can do that.
Runtime.getRuntime().exec("path to the autoIt exe file");
Ex:
Runtime.getRuntime().exec("E:\\Softwares\\Testing\\FileIUploadAutoit.exe");
Here is the small example of uploading a file to website using Selenium WebDriver java using TestNG
public class autoitclass {
public WebDriver driver;
#BeforeTest
public void websitemain()
{
System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\Testing\\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String URL = "http://www.megafileupload.com/";
driver.get(URL);
}
#Test
public void uploadFile() throws Throwable{
driver.findElement(By.xpath(".//a[contains(#class,'slider-btn')]")).click();
driver.findElement(By.xpath(".//*[#id='initialUploadSection']")).click();
Runtime.getRuntime().exec("E:\\Softwares\\Testing\\FileIUploadAutoit.exe");
}
#AfterTest
public void quit(){
driver.quit();
}

You cannot test standalone apps(desktop) using selenium(Some me please correct me if incorrect),exception being apps developed using Electron. When you compile and build an electron project you get an Exe which selenium can interact and test.
The following example demo how to interact :
public void TestSampleGooglePlayElectronApp()
{
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.BinaryLocation = #"C:\MySampleElectronApp\MyApp.exe";
DesiredCapabilities capability = new DesiredCapabilities();
capability.SetCapability(CapabilityType.BrowserName, "Chrome");
capability.SetCapability("chromeOptions", chromeOptions);
IWebDriver driver = new ChromeDriver(chromeOptions);
driver.FindElement(By.XPath("//paper-button[contains(text(),'Sign in')]")).Click();
}

Related

How to resolve the issue with the browser is being closed when start running testng using Edge driver

I have a couple issues when I run the test with edge web browser.
The first issue is that every time I run the test by run as > testng test , the automation will close all the existing open edge web browser and then it will open a brand new edge browser but it is blank (could not load the url)
The second issue is that the automation would only load the url if I manually close all of the existing edge browser.
Is there a way to fix these issues? please help me thank you in advance.
Here is my code
public class OpenThePageUsingEdge {
public String baseUrl = "https://catalog-qa.baylorgenetics.com/search";
String driverPath = "C:\\Eclipse\\MicrosoftWebDriver.exe";
public WebDriver driver ;
//initiate NameofInsured as the GenerateData class
//GenerateData NameOfAddrecipients;
#BeforeTest
public void setup(ITestContext ctx) {
TestRunner runner = (TestRunner) ctx;
runner.setOutputDirectory("J:\\zzQA Selenium Automation Suite\\Test Results");
}
#Test
public void openThePageusingEdge () throws InterruptedException {
System.out.println("launching Edge browser");
System.setProperty("webdriver.edge.driver", driverPath);
driver = new EdgeDriver();
driver.get(baseUrl);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

How to set Chrome Options when using WebDriverManager?

I'm using Web driver manager to setup chrome driver. When setting up the driver I want to add some chrome options? How can I do it when using web driver manager?
I checked the WebDriverManager API but couldn't find any clue..
As of WebDriverManager 5.x you can instantiate the webDriver directly via the WebDriverManager builder with additional capabilities this way (in java) :
WebDriver driver;
//...
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
//...
//chromeOptions.addArguments(<another-option>);
//...
driver = WebDriverManager.chromedriver().capabilities(chromeOptions).create();
The capabilities method take a Capabilities as param.
Lucky we are, ChromeOptions implements the Capabilities interface.
public void WebDriverManagerTest()
{
//setup the chromedriver using WebDriverManager
WebDriverManager.chromedriver().setup();
//Create Chrome Options
ChromeOptions option = new ChromeOptions();
option.addArguments("--test-type");
option.addArguments("--disable-popup-bloacking");
DesiredCapabilities chrome = DesiredCapabilities.chrome();
chrome.setJavascriptEnabled(true);
option.setCapability(ChromeOptions.CAPABILITY, option);
//Create driver object for Chrome
WebDriver driver = new ChromeDriver(option);
//Navigate to a URL
driver.get("http://toolsqa.com");
//quit the browser
driver.quit();
}
Found the answer.. Check above!
This is the example code:
public class Test1{
#Test
public void WebDriverManagerTest()
{
//setup the chromedriver using WebDriverManager
WebDriverManager.chromedriver().setup();
//Create driver object for Chrome
WebDriver driver = new ChromeDriver();
//Navigate to a URL
driver.get("http://toolsqa.com");
//quit the browser
driver.quit();
}
}
from https://pypi.org/project/webdriver-manager/, pass it in after .install()
from selenium import webdriver
from webdriver_manager.opera import OperaDriverManager
options = webdriver.ChromeOptions()
options.add_argument('allow-elevated-browser')
options.binary_location = "C:\\Users\\USERNAME\\FOLDERLOCATION\\Opera\\VERSION\\opera.exe"
driver = webdriver.Opera(executable_path=OperaDriverManager().install(), options=options)

Web don't run immediately when using WebDriver driver.get(""")

I just have learned about selenium.
My code:
public class FirstTest {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://mail.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("Email")).sendKeys(" YOUR USER NAME");
driver.findElement(By.id("Passwd")).sendKeys("YOUR PASSWORD");
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.id("signIn")).click();
driver.findElement(By.xpath("//div[#class='z0']/div")).click();
driver.findElement(By.xpath("//div[#class='gb_1 gb_3a gb_nc gb_e']/div/a")).click();
driver.findElement(By.xpath("//*[#id='gb_71']")).click();
driver.close();
}
}
Issue is Firefox didnt run. I use Firefox 52.0.1 and Selenium 3.8.1 Anyone can check why for me !
After click run as java application:

IE goes to http://localhost:36559/ with message "This is the initial start page for the WebDriver server"

I am trying to launch IE but nothing happens with Selenium Java code below:
protected WebDriver ieBrowserSetUp() throws Exception {
// Read the properties file
Properties prop = getProperties();
System.setProperty("webdriver.ie.driver", prop.getProperty("IEDriverPath"));
driver = new InternetExplorerDriver();
driver.navigate().to("https://www.google.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "");
WebDriver driver= new InternetExplorerDriver(caps);
System.out.println("Internet Explorer Browser Setup Done Successfully");
return driver;
}
It is working fine with the Firefox and Chrome Webdriver. I saw in one of the posts the Protected Mode Settings needs to be changed. Is there a workaround as the settings is disabled at work considering I am not a Windows admin. The path is pointed to IEDriverServer.exe I downloaded instead of the "Windows-installed-and-defaulted" IE application.
I solved it with this:
System.setProperty("webdriver.ie.driver", prop.getProperty("IEDriverPath"));
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); //disable protected mode settings
caps.setCapability("initialBrowserUrl", url);
driver = new InternetExplorerDriver(caps);

Not able to run selenium script using phantomjs driver

I am new to phantomjs driver, I need to run my script in background using phantomjs headless driver.
Here is my code i am getting null-pointer exception.
currently am using selenium 2.32,testNG,phantomjs jar 1.0.3
public class PhantomjsDemo {
public WebDriver driver;
#BeforeMethod
public void setup(){
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability("takesScreenshot", true);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"C:\\phantomjs-1.9.2-windows\\phantomjs.exe");
WebDriver driver = new PhantomJSDriver(caps);
driver.get("www.google.com");
}
#Test
public void google(){
driver.findElement(By.xpath("//*[#id='gbqfba']")).getText();
driver.findElement(By.xpath("//*[#id='gbqfba']")).getSize().getHeight();
driver.findElement(By.xpath("//*[#id='gbqfba']")).getSize().getWidth();
driver.findElement(By.xpath("//*[#id='gbqfba']")).click();
}
#AfterMethod
public void close(){
driver.quit();
}
}
You are not initializing your Webdriver member variable in the setup() method, but a method variable:
WebDriver driver = new PhantomJSDriver(caps);
Change it to
this.driver = new PhantomJSDriver(caps);
and the NPE should go away.

Categories

Resources