Different Screen Shot Resolution in Different Browsers - selenium-webdriver

I am facing a problem related to my Project of GUI comparison...
It takes Screen Shots of given URL in different Browsers, but these Screen Shots are having different Resolution for different Browser.
So, my problem is that now what to do for getting the Same Resolution of all the screen shots in different Browsers.???
If any solution is there then kindly tell me.
Detail:
Resolutions With:
Mozilla Firefox:- 1345*627
Google Chrome:- 1345*659
Internet Explorer:- 1345*679
Tools used:
Selenium Web Driver.
Java

Try maximising the driver window
Example with Junit & webdriver:
public class Untitled {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.google.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
#Test
public void testUntitled() throws Exception {
driver.get(baseUrl);
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}`

Related

Multiple screenshot using selenium webdriver

What are the steps to take multiple screenshots in selenium without updating the previous screenshot?
Use this:
class Main {
public static void main(String [] args) throws Exception {
WebDriver driver = new ChromeDriver(); driver.manage().window().maximize();
driver.get("irctc.co.in/");
Date d =new Date();
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("E:\\selenium\\screenshot\\"+d.toString().replace(":", "_")+".png"));
}
}

org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: Cannot click on element

Getting below exception error while running the code in IE browser. However same works fine Firefox and Chrome browser. Can someone help?
I am trying to run the below code in IE browser and trying to click on link "create button" on page https://en.wikipedia.org/wiki/Selenium_%28software%29. But link never gets clicked , same code works fine in Firefox.
can you please advise if I need to make any changes
public class ClickIE {
public static void main(String[] args) throws InterruptedException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("requireWindowFocus", true);
System.setProperty("webdriver.ie.driver", "C:\\Users\\Nitin\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver(caps);
driver.get("https://en.wikipedia.org/wiki/Selenium_%28software%29");]
Thread.sleep(5000);
driver.manage().window().maximize();
driver.findElement(By.xpath("/html/body/div[4]/div[1]/div[1]/ul/li[4]/a")).click();
String url = driver.getCurrentUrl();
if(url.contains("wikipedia")) {
System.out.println(url+" its a internal link - Passed");
}
else {
System.out.println(url+" its a external link - Failed");
}

Appium : NullPointerException coming

I am trying to run the below code to automate my mobile application and every thing seems OK but I am getting NullPointerException on following line:
driver.findElement(By.id("com.app.aftertax.aftertax:id/Text7"));
Here is my code:
public class Login {
public static AndroidDriver driver;
#BeforeTest
public void setUp() throws Exception {
File classpathRoot = new File(System.getProperty("user.dir"));
File app = new File("/Users/hanan/Downloads/app-at.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("platformVersion","4.4");
capabilities.setCapability("app-package", "com.mobikwik_new");
capabilities.setCapability("app-activity", ".MobikwikMain");
capabilities.setCapability("app-wait-activity",
".MobikwikMain");
capabilities.setCapability("appiumVersion", "1.3.7");
//capabilities.setCapability("name", methodName.getName());
capabilities.setCapability("app", app.getAbsolutePath());
new AndroidDriver( new URL("http://localhost:4723/wd/hub"),
capabilities);
}
#Test
public void apiDemo() throws InterruptedException {
Thread.sleep(10000);
WebElement login = driver.findElement(By.id("com.app.aftertax.aftertax:id/Text7"));
login.click();
}
}
If your code line is the first in the stack trace (you should add the full stacktrace to such questions), then it's the driver variable which is not initialized (==null)
It seems that you are not assigning Appium object in below code:
new AndroidDriver( new URL("http://localhost:4723/wd/hub"),
capabilities);
You should create reference variable of AppiumDriver or AndroidDriver at class level. then assign created object in reference variable like below:
driver = new AndroidDriver( new URL("http://localhost:4723/wd/hub"),
capabilities);

How can I run Fluentlenium Code inside Selenium Webdriver Firefox Driver?

I am having issues trying to get my Fluentlenium code to run inside the WebDriver Firefox Driver. I need Fluentlenium to execute inside the WebDriver Firefox Driver instead of opening it's own browser. I think I need to override this but I am not exactly sure how to do this. Any help would greatly appreciated. Thanks! Here is what I have for code:
WebDriver driver = new FirefoxDriver();
#Test
public void create_a_picklist()
{
// Go to Page
goTo("http://www.google.com");
}
What happens is that it opens two browsers. One is from the Firefox Driver and the other must be the default browser from the goTo from Fluentlenium. I need it to run this code inside the Firefox Driver window and not open it's own window from Fluentlenium.
By default, it launchs a Firefox browser so that's sufficient :
public class Test extends FluentTest {
#Test
public void go_to_google()
{
goTo("http://www.google.com");
}
}
And nothing more :)
Ok. Looks like I figured it out. Here is what I did to override the browser:
public class Test extends FluentTest {
// Defines the Driver
public WebDriver driver = new FirefoxDriver();
// Overrides the default driver
#Override
public WebDriver getDefaultDriver() {
return driver;
}
#Test
public void go_to_google()
{
goTo("http://www.google.com");
}
}

Alert doesn't close using Selenium WebDriver with Google Chrome.

I have the following Selenium script for opening alert on rediff.com:
public class TestC {
public static void main(String[] args) throws InterruptedException, Exception {
System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.rediff.com/");
driver.findElement(By.xpath("//*[#id='signin_info']/a[1]")).click();
driver.findElement(By.id("btn_login")).click();
Thread.sleep(5000);
Alert alert=driver.switchTo().alert();
alert.accept();
}
}
This very same script is working fine in Firefox and IE9, however using Google Chrome after opening the alert, rest of the code is not working. The main thing is that does not shows any exception, error or anything.
Please provide any solution as soon as possible.
Thanks a lot!
Note: If we need to change any setting of browser or any thing please let me know.
Selenium version:Selenium(2) Webdriver
OS:Windows 7
Browser:Chrome
Browser version:26.0.1410.64 m
I'm pretty sure your problem is a very common one, that's why i never advise using Thread.sleep(), since it does not guarantee the code will run only when the Alert shows up, also it may add up time to your tests even when the alert is shown.
The code below should wait only until some alert is display on the page, and i'd advise you using this one Firefox and IE9 aswell.
public class TestC {
public static void main(String[] args) throws InterruptedException, Exception {
System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, 5);
driver.get("http://www.rediff.com/");
driver.findElement(By.xpath("//*[#id='signin_info']/a[1]")).click();
driver.findElement(By.id("btn_login")).click();
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
}
}
Mostly all that is done here, is changing Thread.sleep(), for a condition that actually will only move forward on the code as soon a alert() is present in the page. As soon as someone does, it wil switch to it and accept.
You can find the Javadoc for the whole ExpectedConditions class here.
Unfortunately AlertIsPresent doesn't exist in C# API
http://selenium.googlecode.com/git/docs/api/dotnet/index.html
You can use something like this:
private static bool TryToAcceptAlert(this IWebDriver driver)
{
try
{
var alert = driver.SwitchTo().Alert();
alert.Accept();
return true;
}
catch (Exception)
{
return false;
}
}
public static void AcceptAlert(this IWebDriver driver, int timeOutInSeconds = ElementTimeout)
{
new WebDriverWait(driver, TimeSpan.FromSeconds(timeOutInSeconds)).Until(
delegate { return driver.TryToAcceptAlert(); }
);
}

Resources