file upload

classic Classic list List threaded Threaded
27 messages Options
12
Reply | Threaded
Open this post in threaded view
|

file upload

zlos7ar
I am using selenium server standalone 2.21 and java and I am looking for a good file upload solution that works for chrome and ie. Anyone have an idea about this ?
Reply | Threaded
Open this post in threaded view
|

Re: file upload

softwaretestingforum
Administrator
Did you try sendKeys methods?
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: file upload

zlos7ar
yep but it works only in firefox

driver.findElement(By.xpath("//*[@id='content-container']/div[4]/div[2]/form/input")).sendKeys("C:\\Users\\Public\\Pictures\\Sample Pictures\\Jellyfish.jpg");

Reply | Threaded
Open this post in threaded view
|

Re: file upload

softwaretestingforum
Administrator
Do you encounter any exception?
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: file upload

zlos7ar
in chrome it says that the element is not clickable......

org.openqa.selenium.WebDriverException: Element is not clickable at point (-49.5, 460.5) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 68 milliseconds
Build info: version: '2.21.0', revision: '16552', time: '2012-04-11 19:08:38'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_02'
Driver info: driver.version: RemoteWebDriver
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:175)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:128)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:459)
        at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:245)
        at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:88)
        at chrome.SubmitIdea.testSubmitIdea(SubmitIdea.java:44)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Reply | Threaded
Open this post in threaded view
|

Re: file upload

softwaretestingforum
Administrator
This looks like an intermittent error in chrome. Do you always encounter this error in chrome browser?
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: file upload

zlos7ar
yes :)
Reply | Threaded
Open this post in threaded view
|

Re: file upload

softwaretestingforum
Administrator
In reply to this post by softwaretestingforum
Look like you need to use safeClick method. Try this instead of click -

public void safeClick(By locator) {
 int i=0
 while (i<30) {
  try {
   driver.findElement(locator).click()
  break;
 }catch(Exception e) {
   e.printStackTrace();
   i=i+1;
  Thread.sleep(1000);
 }
 }
}
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: file upload

zlos7ar
thanks but i have no idea how to use this code... i still have a lot to learn about java and webdriver. Can you give me an example or something ?
Reply | Threaded
Open this post in threaded view
|

Re: file upload

softwaretestingforum
Administrator
Could you post your test code with which you get the exception and I would fit in my code in that.
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: file upload

zlos7ar
here it is the part with the picture upload is commented :


package chrome;

import static org.junit.Assert.fail;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class SubmitIdea {
        private WebDriver driver;
        private String baseUrl;
        private StringBuffer verificationErrors = new StringBuffer();
       
        @Before
        public void setUp() throws Exception {
                System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");
                driver = new ChromeDriver();
                baseUrl = "https://website.com";
        }

        @Test
        public void testSubmitIdea() throws Exception {
                driver.get(baseUrl + "/login.php");
                driver.findElement(By.name("login_user")).clear();
                driver.findElement(By.name("login_user")).sendKeys("admin");
                driver.findElement(By.name("login_pw")).clear();
                driver.findElement(By.name("login_pw")).sendKeys("password");
                driver.findElement(By.cssSelector("div.center > input[name=\"submit\"]")).click();
                driver.findElement(By.linkText("Idee Einreichen")).click();
                for (int second = 0;; second++) {
                        if (second >= 60) fail("timeout");
                        try { if (isElementPresent(By.xpath("//span[text()='close']"))) break; } catch (Exception e) {}
                        Thread.sleep(1000);
                }
                driver.findElement(By.xpath("//span[text()='close']")).click();
// driver.findElement(By.xpath("//*[@id='content-container']/div[4]/div[2]/form/input")).sendKeys("C:\\Users\\Public\\Pictures\\Sample Pictures\\Jellyfish.jpg");
// for (int second = 0;; second++) {
// if (second >= 60) fail("timeout");
// try { if (isElementPresent(By.xpath("//span[text()='Save']"))) break; } catch (Exception e) {}
// Thread.sleep(1000);
// }
// driver.findElement(By.xpath("//span[text()='Save']")).click();
                Thread.sleep(3000);
                driver.findElement(By.name("title")).clear();
                driver.findElement(By.name("title")).sendKeys("idea testing");
                ((JavascriptExecutor)driver).executeScript("document.getElementsByName('title')[0].blur");
                driver.findElement(By.name("input_0")).clear();
                driver.findElement(By.name("input_0")).sendKeys("blabla bla");
                ((JavascriptExecutor)driver).executeScript("document.getElementsByName('input_0')[0].blur");
                new Select(driver.findElement(By.name("cfi"))).selectByVisibleText("Wauzi");
                driver.findElement(By.xpath("//*[@id='content-container']/div[5]/div[4]/label[1]/input")).click();
                driver.findElement(By.xpath("//*[@id='content-container']/div[5]/div[4]/label[3]/input")).click();
                driver.findElement(By.xpath("//*[@id='content-container']/div[5]/div[4]/label[7]/input")).click();
                driver.findElement(By.xpath("//*[@id='content-container']/div[5]/input[2]")).clear();
                driver.findElement(By.xpath("//*[@id='content-container']/div[5]/input[2]")).sendKeys("profsgg, fsdfsdfs, fsdfghhhrt");
                ((JavascriptExecutor)driver).executeScript("document.getElementsByName('tags')[0].blur");
                driver.findElement(By.xpath("//input[@class='attachments']")).sendKeys("C:\\Users\\Public\\Pictures\\Sample Pictures\\Koala.jpg");
                Thread.sleep(1000);
                driver.findElement(By.xpath("//div[text()='Ideengeber hinzuf├╝gen']")).click();
                for (int second = 0;; second++) {
                        if (second >= 60) fail("timeout");
                        try { if (isElementPresent(By.xpath("//*[@id='popup']/div/div/input"))) break; } catch (Exception e) {}
                        Thread.sleep(1000);
                }

                driver.findElement(By.xpath("//*[@id='popup']/div/div/input")).sendKeys("Viktor Georgiev");
                for (int second = 0;; second++) {
                        if (second >= 60) fail("timeout");
                        try { if (isElementPresent(By.xpath("//*[@id='popup']/div/ul/li[1]/div"))) break; } catch (Exception e) {}
                        Thread.sleep(1000);
                }

                driver.findElement(By.xpath("//*[@id='popup']/div/ul/li[1]/div")).click();
                for (int second = 0;; second++) {
                        if (second >= 60) fail("timeout");
                        try { if (isElementPresent(By.xpath("//*[@id='content-container']/div[5]/div[7]/div[1]/span/a"))) break; } catch (Exception e) {}
                        Thread.sleep(1000);
                }
                driver.findElement(By.xpath("//div[text()='Idee Einreichen']")).click();
                for (int second = 0;; second++) {
                        if (second >= 60) fail("timeout");
                        try { if (isElementPresent(By.xpath("//span[text()='close']"))) break; } catch (Exception e) {}
                        Thread.sleep(1000);
                }
                Thread.sleep(2000);
                driver.findElement(By.xpath("//span[text()='close']")).click();
                for (int second = 0;; second++) {
                        if (second >= 60) fail("timeout");
                        try { if (isElementPresent(By.xpath("//*[@id='content-container']/div[1]/div[1]/big"))) break; } catch (Exception e) {}
                        Thread.sleep(1000);
                }
                driver.findElement(By.xpath("//*[@id='header-container']/div/div[4]/a[2]")).click();
        }

        @After
        public void tearDown() throws Exception {
                driver.quit();
                String verificationErrorString = verificationErrors.toString();
                if (!"".equals(verificationErrorString)) {
                        fail(verificationErrorString);
                }
        }

        private boolean isElementPresent(By by) {
                try {
                        driver.findElement(by);
                        return true;
                } catch (NoSuchElementException e) {
                        return false;
                }
        }
}
Reply | Threaded
Open this post in threaded view
|

Re: file upload

softwaretestingforum
Administrator
I suppose this is the line which throws exception -

driver.findElement(By.xpath("//span[text()='Save']")).click();

so instead of this call safeClick method as -

safeClick(By.xpath("//span[text()='Save']"))

and write safeClick method some where in your class -

public void safeClick(By locator) {
 int i=0
 while (i<30) {
  try {
   driver.findElement(locator).click()
  break;
 }catch(Exception e) {
   e.printStackTrace();
   i=i+1;
  Thread.sleep(1000);
 }
 }
}

Does it make sense?

I would also suggest you to wrap your WebDriver calls using page object so that maintenance would be easy. You could refer this post for more -

http://www.seleniumtests.com/2012/01/selenium-2-methods-are-no-more-weird.html
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: file upload

zlos7ar
nope this is the line which throws exception:
driver.findElement(By.xpath("//*[@id='content-container']/div[4]/div[2]/form/input")).sendKeys("C:\\Users\\Public\\Pictures\\Sample Pictures\\Jellyfish.jpg");
Reply | Threaded
Open this post in threaded view
|

Re: file upload

softwaretestingforum
Administrator
This post was updated on .
In that case use it as -

public void safeSendKeys(By locator, String testdata) {
 int i=0
 while (i<30) {
  try {
   driver.findElement(locator).sendKeys(testdata)
  break;
 }catch(Exception e) {
   e.printStackTrace();
   i=i+1;
  Thread.sleep(1000);
 }
 }
}

and call it as -

sendKeys(By.xpath("//*[@id='content-container']/div[4]/div[2]/form/input") , "C:\\Users\\Public\\Pictures\\Sample Pictures\\Jellyfish.jpg");
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: file upload

zlos7ar
can you tell me what should be the syntax for the safeSendKeys line.
thanks
Reply | Threaded
Open this post in threaded view
|

Re: file upload

softwaretestingforum
Administrator
I have modified my previous response. This should help.
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: file upload

zlos7ar
the method safeSendKeys(by) in the type SubmitIdea is not applicable for the arguments (By, String)
safeSendKeys(By.xpath("//*[@id='content-container']/div[4]/div[2]/form/input") , "C:\\Users\\Public\\Pictures\\Sample Pictures\\Jellyfish.jpg");

and safeSendKeys is underlined in eclipse so i guess that the syntax is not correct.
if i make it like this:
SendKeys(By.xpath("//*[@id='content-container']/div[4]/div[2]/form/input") , "C:\\Users\\Public\\Pictures\\Sample Pictures\\Jellyfish.jpg");
i get the same error message....the method safeSendKeys(by) in the type SubmitIdea is not applicable for the arguments (By, String).....
Reply | Threaded
Open this post in threaded view
|

Re: file upload

softwaretestingforum
Administrator
Could you post a snapshot of error you see in IDEA or Eclipse?
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: file upload

zlos7ar

Reply | Threaded
Open this post in threaded view
|

Re: file upload

softwaretestingforum
Administrator
And where did you write safeSendKeys method I posted earlier.
It should be like your - testSubmitIdea method but a different method.

You get this error because IDEA/Eclipse can not find the corresponding implementation for safeSendKeys. You are just calling safeSendKeys but where is it defined? Do you get my point?
~ seleniumtests.com
12