Found 871 Articles for Automation Testing

Access to file download dialog in Firefox in Selenium.

Debomita Bhattacharjee
Updated on 28-Dec-2020 13:00:05

446 Views

We can access file download dialog in Firefox in Selenium. For this we have to first modify the default directory where the downloaded file gets stored. This is done by the addpreference method.p.addPreference("browser.download.folderList", 2);Then, define the new path of the download directory.Finally, we shall ignore the save to disk and open file options in dialog for the file types via their MIME code. The addPreference method can be called with the help of FirefoxOptions class.ExampleCode Implementation.import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import java.util.concurrent.TimeUnit; public class FileDwnloadWithoutDialg{    public static void main(String[] args) {       System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe"); ... Read More

How to set default download directory in selenium Chrome Capabilities?

Debomita Bhattacharjee
Updated on 28-Dec-2020 12:57:25

3K+ Views

We can set the default download directory in Selenium with Chrome capabilities. All the browsers have a download directory set by default. We can modify it via the browser settings.We can change the setting manually, but it gets modified on triggering a script. We change the directory for download in Chrome browser with the help of ChromeOptions class.We are required to add capabilities to the browser with the parameter download.default_directory. The path of the new location to be set is considered as its value.ExampleCode Implementation.import java.io.File; import org.openqa.selenium.By; import java.io.IOException; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import java.util.HashMap; import java.util.Map; public class SetDownloadPathChrome ... Read More

How can I download a file on a click event using selenium?

Debomita Bhattacharjee
Updated on 28-Dec-2020 12:54:51

1K+ Views

We can download a file on a click event in Selenium. Normally, on clicking on the download link, a pop-up comes with the file name along with the Open and Save option.To download with a click event, an object of the FirefoxOptions class is to be created. Then with the addPreference method, we shall configure browser preferences.We shall mention the location where the download is to be done along with the neverAsk.openFile and neverAsk.saveToDisk preferences are to be set.ExampleCode Implementation.import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import java.util.concurrent.TimeUnit; public class FileDwnloadWithClick{    public static void main(String[] args) {   ... Read More

How do I switch to the active tab in Selenium?

Debomita Bhattacharjee
Updated on 28-Dec-2020 12:00:36

5K+ Views

We can switch to the active tab in Selenium. The methods Keys.chord and sendKeys are used to open a new tab. More than one key can be passed at once with the Keys.chord method.The Keys.CONTROL and Keys.ENTER combined are passed as parameters to the Keys.chord method. This is stored as a string and again passed as a parameter to the sendKeys method.SyntaxString tb = Keys.chord(Keys.CONTROL, Keys.ENTER); driver.findElement(By.xpath("//*[text()='Company']")).sendKeys(tb);All the window ids which are opened are stored in an array list. By default, the driver has the focus on the parent window. To switch the focus to the new tab, switchTo().window method is used.The ... Read More

How to download any file and save it to the desired location using Selenium Webdriver?

Debomita Bhattacharjee
Updated on 28-Dec-2020 11:58:07

3K+ Views

We can download any file and save it to the desired location with Selenium. This can be done by creating an instance of the FirefoxOptions class. Then with the help of the addPreference method, we have to set the browser preferences.We shall also specify the path where the file has to be downloaded with the help of the addPreference method.ExampleCode Implementation.import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import java.util.concurrent.TimeUnit; public class FileDwnload{    public static void main(String[] args) {       System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");       // create object of FirefoxOptions class       FirefoxOptions profile = ... Read More

Checking HTTP Status Code in Selenium.

Debomita Bhattacharjee
Updated on 28-Dec-2020 11:54:54

1K+ Views

We can check HTTP status code in Selenium. As we run tests, we can verify the status code of a response from a resource. Some of the various HTTP status codes are −5XX – Error in server.4XX – Resource not detected.3XX - Redirected.2XX – Ok.An instance of the class HttpURLConnection is used to get the HTTP status code. To have a connection to an URL, the openConnection method shall be utilized. Then we have to take the help of setRequestMethod and pass HEAD as a parameter to that.To establish the connection, the connect method is to be applied to the object ... Read More

How to open a new tab using Selenium WebDriver?

Debomita Bhattacharjee
Updated on 28-Dec-2020 11:50:58

1K+ Views

We can open a new tab with Selenium. The Keys.chord and sendKeys methods are used for this. Multiple keys can be passed simultaneously with the Keys.chord method. A group of strings or keys can be passed as parameters to that method.The Keys.CONTROL and Keys.ENTER are passed as parameters to the Keys.chord method here. This is stored as a string value and finally passed as a parameter to the sendKeys method.SyntaxString nwtb = Keys.chord(Keys.CONTROL, Keys.ENTER); driver.findElement(By.xpath("//*[text()='Company']")).sendKeys(nwtb);ExampleCode Implementation.import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class OpnLinkNwTab{    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");   ... Read More

How to check if an image is displayed on page using Selenium?

Debomita Bhattacharjee
Updated on 28-Dec-2020 11:41:16

10K+ Views

We can check if an image is displayed on page with Selenium. To verify an image we shall take the help of Javascript Executor. We shall utilize executeScript method to execute the Javascript commands in Selenium.Then pass the command return arguments[0].complete && typeof arguments[0].naturalWidth != \"undefined\" && arguments[0].naturalWidth > 0 as a parameter to that method. Moreover, the statement import org.openqa.selenium.JavascriptExecutor has to be added in code.SyntaxWebElement i = driver.findElement(By.xpath("//img[@title='Tutorialspoint']")); Boolean p = (Boolean) ((JavascriptExecutor)driver) .executeScript("return arguments[0].complete " + "&& typeof arguments[0].naturalWidth != \"undefined\" " + "&& arguments[0].naturalWidth > 0", i);Let us check if the below image is present on page −ExampleCode ... Read More

How to get innerHTML of whole page in selenium driver?

Debomita Bhattacharjee
Updated on 28-Dec-2020 11:37:50

2K+ Views

We can get the innerHTML of the whole page in Selenium. We shall use the method getPageSource and print the values captured by it in the console.SyntaxString s = driver.getPageSource();We can also get the HTML source code via Javascript commands in Selenium. We shall utilize executeScript method and pass the command return document.body.innerHTML as a parameter to the method.SyntaxJavascriptExecutor j = (JavascriptExecutor) driver; String s = (String) j.executeScript("return document.body.innerHTML;");ExampleCode Implementation with getPageSource.import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class PageHTMLcode{    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = ... Read More

Getting console.log output from Firefox with Selenium.

Debomita Bhattacharjee
Updated on 28-Dec-2020 11:33:09

1K+ Views

We can get console.log output from Firefox with Selenium. This is done with the setProperty method.FirefoxDriver.SystemProperty.BROWSER_LOGFILE and the path of the file in which the logs are to be captured are passed as parameters to that method.SyntaxSystem.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "FFLogs.txt");After refreshing the project folder, we shall get the FFLogs.txt file where the logs shall be captured.ExampleCode Implementation.import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.logging.LogType; public class FirefoxLogs{    public static void main(String[] args) {       System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");       // set the System Property to BROWSER_LOGFILE and path of log file       System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, ... Read More

Advertisements