Receiving error when Uploading File using HtmlUnit - java

I am currently attempting to automate the upload of a file to a specific site. I am able to successfully login to the site and navigate to the import page; however, when I attempt to import the file, I receive an error. My guess is that it is because I am simply a user and am not granted permissions to write to the server. However, if I perform the import manually, then it is successful. Normally there would be four stages to the import. However, after the first step you can see that an error occurred. My code is listed below along with the error I am receiving:
public static void main(String args[]) throws Exception {
login();
}
#SuppressWarnings("resource")
public static void login() throws Exception {
// Open the webclient using Internet Explorer (Chrome does not work).
final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER);
// Get the first page
final HtmlPage page = webClient.getPage("http://telephone.qqest.com/phone/Login/Login.asp");
// Get the form that we are dealing with.
final HtmlForm loginForm = page.getFormByName("frmLogin");
final HtmlTextInput userName = loginForm.getInputByName("Login");
final com.gargoylesoftware.htmlunit.html.HtmlPasswordInput passWord = (com.gargoylesoftware.htmlunit.html.HtmlPasswordInput)
loginForm.getInputByName("Password");
final HtmlTextInput companyID = loginForm.getInputByName("Ident");
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setRedirectEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getCookieManager().setCookiesEnabled(true);
//final HtmlInput login = loginForm.getInputByName("Login");
// Change the values of the text fields.
userName.setValueAttribute("xxxx");
passWord.setValueAttribute("xxxxx");
companyID.setValueAttribute("xxxxx");
//create a submit button - it doesn't work with 'input'
DomElement loginBtn = page.createElement("button");
loginBtn.setAttribute("type", "submit");
// append the button to the form
loginForm.appendChild(loginBtn);
// submit the form
loginBtn.click();
//navigate to page for import.
HtmlPage page3 = webClient.getPage("http://telephone.qqest.com/phone/Imports/Employee/Module.asp");
//populate the textfield with the specified file.
final HtmlForm importForm = page3.getFormByName("ImportForm");
final HtmlFileInput inputFile = importForm.getInputByName("UploadFile");
inputFile.setValueAttribute("C:\\Users\\thisFile.xls");
inputFile.click();
final HtmlSubmitInput importBtn = (HtmlSubmitInput)importForm.getInputByValue("Import");
try {
importBtn.fireEvent(Event.TYPE_INPUT);
}
catch (NullPointerException ex) {
System.err.println(ex);
}
HtmlPage page4 = webClient.getPage("http://telephone.qqest.com/phone/Imports/Employee/ImportFile.asp?FileType=application/vnd.ms-excel&FilePath=c%3A%5Cinetpub%5Cwwwroot%5Cphone%5CDownloads%5CImports%5C&FileName=thisFile.xls.asp");
System.out.println("Import Page: " + page4.asText());
}
The error I am receiving:
Stage 1 of 4: Upload File - Completed
Microsoft JET Database Engine error '80004005'
Cannot update. Database or object is read-only.
/phone/Imports/Employee/ImportFile.asp, line 155

I have figured out a solution and wanted to post it for those who might find it useful in the future. The problem was that I was simply trying to redirect to the import page by using the URL. Prior I had tried to submit the forms with an importBtn.click() statement; however, I needed to declare this statement as its own HtmlPage variable.
final HtmlSubmitInput importBtn = (HtmlSubmitInput)importForm.getInputByValue("Import");
HtmlPage page4 = importBtn.click();
// loops until the page changes.
while(page4 == page3) {
// The page hasn't changed.
Thread.sleep(500);
}
System.out.println("Import Page: " + page4.asText());

Related

HtmlUnit wait for redirect

I'm having following scenario I need to implement in HtmlUnit:
-I click button
-Im getting redirected to page www.page.pl/result
-I need to wait few seconds and then I'm getting redirected to www.anotherpage.pl/auth_code=qwe123
The problem is, once I'm getting to /result page, I can't get out of it to next one. There are no manual actions there, I just need to wait until redirect happens
Here is my WebClient
private WebClient initWebClient() {
WebClient webClient = new WebClient();
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setRedirectEnabled(true);
return webClient;
}
And how I'm trying to accomplish this, I'v tried several ways, including Thread.sleep() already...
HtmlPage page = webClient.getPage(url);
page.getForms().get(0).getButtonByName("submit").click(); // Redirects me to /result
webClient.waitForBackgroundJavaScript(5000);
webClient.waitForBackgroundJavaScriptStartingBefore(5000);
Thread.sleep(5000);
page.refresh(); //I'm still on /result, while I need to be on completely new page by now
In case anyone else have similar issue, I found solution
webClient.addWebWindowListener(new WebWindowListener() {
#Override
public void webWindowOpened(WebWindowEvent webWindowEvent) {
}
#Override
public void webWindowContentChanged(WebWindowEvent webWindowEvent) {
if (webWindowEvent.getNewPage() != null) {
// do what you need to do with webWindowEvent.getNewPage().getUrl();
}
}
#Override
public void webWindowClosed(WebWindowEvent webWindowEvent) {
}
});

Htmlunit - click() doesn't work

I am spidering a web page ... but click() doesnt work, or doesn't navigate ...
Any clue what could be the issue ? I'm using 2.25 version
you can see all details in the code, thank you very much in advance.
this my code:
#Test
public String xxx() throws Exception {
try (final WebClient webClient = new WebClient(BrowserVersion.getDefault())) {
webClient.setJavaScriptTimeout(15000);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setJavaScriptEnabled(false);
webClient.waitForBackgroundJavaScript(30000);
webClient.getOptions().setActiveXNative(true);
webClient.getOptions().setAppletEnabled(true);
webClient.getOptions().setCssEnabled(true);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setRedirectEnabled(true);
// add to log variable
//toLog.append(" contrato: ").append(numeroContrato);
final HtmlPage consultaCuentaPage = webClient.getPage(url);
// grab first form
final HtmlForm consultarCuentaForm = consultaCuentaPage.getForms().get(0);
// numero cuenta input
final HtmlInput numeroCuentaInput = consultarCuentaForm.getInputByName("nroCuenta");
// consultar button
final HtmlInput consultarButton = consultarCuentaForm.getInputByName("commandConsultar");
// Change the value of the text field
numeroCuentaInput.setValueAttribute("1111");
// Now submit the form by clicking the button and get back the second page.
final HtmlPage consultaCuentaPage2 = consultarButton.click();
LOG.info("time: " + consultaCuentaPage2.getWebResponse().getLoadTime());
// LOG.info("time: " + consultaCuentaPage2.getWebResponse().getContentAsString());
//System.out.println(consultaCuentaPage2.asText());
////*[#id="form"]/div[1]/fieldset/table/tbody/tr[1]/td[1]
if (consultaCuentaPage2.getFirstByXPath("//*[#id=\"form\"]/div[1]/fieldset/table/tbody/tr[1]/td[1]") != null) {
return ((HtmlTableDataCell)consultaCuentaPage2.getFirstByXPath("//*[#id=\"form\"]/div[1]/fieldset/table/tbody/tr[1]/td[1]")).asText();
}
}
return null;
}

Htmlunit button won't submit?

public void actionVote() {
HtmlForm form = this.page.getForms().get(0);
HtmlInput button = form.getInputByValue("vote");
try {
button.click();
} catch (IOException e) {
e.printStackTrace();
}
}
When i do println with button.asText(), it gives me the correct value of the submit button, but when I do button.click, nothing happens, like it doesn't submit the form.
I can't get the button using HtmlButton because the submit button doesn't have any id or name.
I also can't make it HtmlButton from HtmlInput.
Why doesn't this submit? Wrong element?
Try this - getInputByName or getButtonByName
HtmlForm form = this.page.getForms().get(0);
HtmlInput button = form.getInputByName("vote");
Or you can also create a fake button:
HtmlElement fakeButton = page.createElement("button");
button.setAttribute("type", "submit");
// add the button to the form
form.appendChild(fakeButton );
fakeButton.click();
Can you try below code:
HtmlForm form = page.getForms().get(0);
HtmlElement input = form.getElementsByAttribute("input", "name", "vote").get(0);
page = input.click();

Selenium Username Password Code Error

I am writing a code that enters the username and password into a URL and submits that page. but I keep getting this error
"Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException:
Unable to locate element: {"method":"name","selector":"username"}"
Below is the code
package org.openqa.selenium.example;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class LoginPage {
private final WebDriver driver;
public LoginPage(WebDriver driver) {
this.driver = driver;
// Check that we're on the right page.
if (!"Outreach Configuration".equals(driver.getTitle())) {
// Alternatively, we could navigate to the login page, perhaps logging out first
throw new IllegalStateException("This is not the login page");
}
}
// The login page contains several HTML elements that will be represented as WebElements.
// The locators for these elements should only be defined once.
// By usernameLocator = By.name("username");
// By passwordLocator = By.name("password");
// By loginButtonLocator = By.name("submit");
// The login page allows the user to type their username into the username field
public LoginPage typeUsername(String username) {
// This is the only place that "knows" how to enter a username
driver.findElement(By.name("username")).sendKeys(username);
// Return the current page object as this action doesn't navigate to a page represented by another PageObject
return this;
}
// The login page allows the user to type their password into the password field
public LoginPage typePassword(String password) {
// This is the only place that "knows" how to enter a password
//driver.findElement(passwordLocator).sendKeys(password);
driver.findElement(By.name("password")).sendKeys(password);
// Return the current page object as this action doesn't navigate to a page represented by another PageObject
return this;
}
// The login page allows the user to submit the login form
public HomePage submitLogin() {
// This is the only place that submits the login form and expects the destination to be the home page.
// A seperate method should be created for the instance of clicking login whilst expecting a login failure.
// driver.findElement(By.name("submit")).submit();
// Return a new page object representing the destination. Should the login page ever
// go somewhere else (for example, a legal disclaimer) then changing the method signature
// for this method will mean that all tests that rely on this behaviour won't compile.
return new HomePage(driver);
}
// The login page allows the user to submit the login form knowing that an invalid username and / or password were entered
public LoginPage submitLoginExpectingFailure() {
// This is the only place that submits the login form and expects the destination to be the login page due to login failure.
// driver.findElement(By.name("submit")).submit();
// Return a new page object representing the destination. Should the user ever be navigated to the home page after submiting a login with credentials
// expected to fail login, the script will fail when it attempts to instantiate the LoginPage PageObject.
return new LoginPage(driver);
}
// Conceptually, the login page offers the user the service of being able to "log into"
// the application using a user name and password.
public HomePage loginAs(String username, String password) {
// The PageObject methods that enter username, password & submit login have already defined and should not be repeated here.
typeUsername(username);
typePassword(password);
return submitLogin();
}
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
// And now use this to visit Google
driver.get("URL Goes Here");
LoginPage login = new LoginPage(driver);
HomePage a=login.loginAs("username","Password");
}
}
I reffered it from http://code.google.com/p/selenium/wiki/PageObjects
Try to add
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
before HomePage a=login.loginAs("username","Password");.
It is not quite good approach, but it will help to find out where is the problem. This is just a pause before next step. You shouldn't use it in future, because it is not pretty good practice. It is better to use wait for condition check this out for more information.

Problem in HtmlUnit API for Java (Headless Browser)?

I am using HtmlUnit headless browser to browse this webpage (you can see the webpage to have a better understanding of the problem).
I have set the select's value to "1"
by the following commands
final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_7);
try {
// Configuring the webClient
webClient.setJavaScriptEnabled(true);
webClient.setThrowExceptionOnScriptError(false);
webClient.setCssEnabled(true);
webClient.setUseInsecureSSL(true);
webClient.setRedirectEnabled(true);
webClient.setActiveXNative(true);
webClient.setAppletEnabled(true);
webClient.setPrintContentOnFailingStatusCode(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
// Adding listeners
webClient.addWebWindowListener(new com.gargoylesoftware.htmlunit.WebWindowListener() {
public void webWindowOpened(WebWindowEvent event) {
numberOfWebWindowOpened++;
System.out.println("Number of opened WebWindow: " + numberOfWebWindowOpened);
}
public void webWindowContentChanged(WebWindowEvent event) {
}
public void webWindowClosed(WebWindowEvent event) {
numberOfWebWindowClosed++;
System.out.println("Number of closed WebWindow: " + numberOfWebWindowClosed);
}
});
webClient.setWebConnection(new HttpWebConnection(webClient) {
public WebResponse getResponse(WebRequestSettings settings) throws IOException {
System.out.println(settings.getUrl());
return super.getResponse(settings);
}
});
CookieManager cm = new CookieManager();
webClient.setCookieManager(cm);
HtmlPage page = webClient.getPage("http://www.ticketmaster.com/event/0B004354D90759FD?artistid=1073053&majorcatid=10002&minorcatid=207");
HtmlSelect select = (HtmlSelect) page.getElementById("quantity_select");
select.setSelectedAttribute("1", true);
and then clicked on the following button
by the following commands
HtmlButtonInput button = (HtmlButtonInput) page.getElementById("find_tickets_button");
HtmlPage captchaPage = button.click();
Thread.sleep(60*1000);
System.out.println("======captcha page=======");
System.out.println(captchaPage.asXml());
but even after clicking on the button and waiting for 60 seconds through the Thread.sleep() method, I am getting the same HtmlPage.
But when I do the same thing through real browser then I get the page that contains CAPTCHA.
I think I am missing something in the htmlunit.
Q1. Why am I not getting the same page (that contains CAPTCHA) through htmlunit's browser?
The web form on that page requires the quantity_select drop-down to be filled in. You're attempting to do that in your code by assuming the drop-down is a select element. However, it's no longer a select element. Try using Firebug to inspect the drop-down and you'll see that JavaScript has replaced the select with a complex set of nested div elements.
If you figure out how to emulate each user click on the divs for that unusual drop-down then you should be able to submit the form.

Categories

Resources