Multiple screenshot using selenium webdriver - 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"));
}
}

Related

Is there any way to enter data in text field without giving webelement

I need to send data on the highlighted tab, but don't want to pass data by findElement. My code is listed below. Please advise.
public class A003_KeyBoardActions {
public static void main(String[] args) throws Exception{
System.setProperty("WebDriver.driver","C:eclipse\\IEDriverServer.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("URL");
Robot r=new Robot();
r.keyPress(java.awt.event.KeyEvent.VK_TAB);
r.keyRelease(java.awt.event.KeyEvent.VK_TAB);
System.out.println("Cursor moved to home page");
r.keyPress(java.awt.event.KeyEvent.VK_TAB);
r.keyRelease(java.awt.event.KeyEvent.VK_TAB);
System.out.println("Courser moved to username");
**HERE I NEED TO SEND DATA ON HIGHLIGHTED TAB** I dont want to pass data by
findElement.
Thread.sleep(1000);
driver.close();
}
}
You can do something like this.
String userName = "YourGoodName";
StringSelection stringSelection = new StringSelection(userName);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);

I'm trying to launch a chromedriver in seleniumwebdriver even i give the right path its show error

public class sampledr {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//Thread.sleep(3000);
System.setProperty(" webdriver.chrome.driver","C:\\Program Files (x86)\\Common Files\\chromedriver.exe" );
WebDriver d= new ChromeDriver();
It's show illegal state exception.
Your code was almost perfect but have a issue. When you mention any Key-Value pair through System.setProperty, these values are String and shouldn't contain any spaces within. So you need to update the line as :
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Common Files\\chromedriver.exe" );
public class sampledr {
public WebDriver driver; //it creates the driver object
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Common Files\\chromedriver.exe" ); //set driver path
wd = new ChromeDriver(); //Call driver
driver.get("https://www.google.com"); //open site

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);

Unable to use keyDown and keyUp events using selenium Webdriver

I want to do the following, can someone help me what wrong i have done..
1) open the site mentioned in code
2) Enter the Text "WELcoME" (mixture of capital and small letters) using keydown and keyup events in webdriver.
public class KeysUpandDown {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://fleet.idrivesafely.com");
driver.manage().window().maximize();
driver.findElement(By.linkText("Student Login")).click();
WebElement loc=driver.findElement(By.className("input1"));
Actions a= new Actions(driver);
a.moveToElement(loc)
.keyDown(Keys.SHIFT)
.sendKeys("wel")
.keyUp(Keys.SHIFT)
.sendKeys("co")
.keyDown(Keys.SHIFT)
.sendKeys("me");
a.perform();
}
}
Your code requires 2 small changes.
Change 1:
WebElement loc=driver.findElement(By.className("input1"));
is pointing to three Web-elements on the page, Instead use below
WebElement loc=driver.findElement(By.xpath("//input[#class='input1' and #name='pin_no']"));
Change 2: Its a Text field, so Instead of
a.moveToElement(loc)
Use
a.click(loc)
So you code should be as below :
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://fleet.idrivesafely.com");
driver.manage().window().maximize();
driver.findElement(By.linkText("Student Login")).click();
WebElement loc=driver.findElement(By.xpath("//input[#class='input1' and #name='pin_no']"));
Actions a= new Actions(driver);
a.click(loc).keyDown(Keys.SHIFT).sendKeys("wel").keyUp(Keys.SHIFT).sendKeys("co").keyDown(Keys.SHIFT).sendKeys("me").perform();

Different Screen Shot Resolution in Different Browsers

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);
}
}`

Resources