selenium webdriver: upload file by drag and drop - selenium

On a web page I'm trying to test, we implemented drag and drop file upload. I've looked at the drag and drop API for selenium action chain API. It looks like it supports only dragging and dropping between 2 elements on a page. How to emulate dragging from a file manager?

To perform an HTML5 file drop with Selenium:
static final String JS_DROP_FILE =
"var tgt=arguments[0],e=document.createElement('input');e.type='" +
"file';e.addEventListener('change',function(event){var dataTrans" +
"fer={dropEffect:'',effectAllowed:'all',files:e.files,items:{},t" +
"ypes:[],setData:function(format,data){},getData:function(format" +
"){}};var emit=function(event,target){var evt=document.createEve" +
"nt('Event');evt.initEvent(event,true,false);evt.dataTransfer=da" +
"taTransfer;target.dispatchEvent(evt);};emit('dragenter',tgt);em" +
"it('dragover',tgt);emit('drop',tgt);document.body.removeChild(e" +
");},false);document.body.appendChild(e);return e;";
WebDriver driver = new FirefoxDriver();
driver.get("http://html5demos.com/file-api");
WebElement drop_area = driver.findElement(By.id("holder"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(JS_DROP_FILE, new Object[] {
drop_area
}).sendKeys("C:\\image.png");

AutoIt has the DLL wrapper. I use it directly from C#/Selenium code.

Drop file using jsExecutor
public void dropFile(File filePath, WebElement target) {
if (!filePath.exists())
throw new WebDriverException("File not found: " + filePath.toString());
JavascriptExecutor jse = (JavascriptExecutor) getDriver();
String JS_DROP_FILE =
"var target = arguments[0]," +
" offsetX = arguments[1]," +
" offsetY = arguments[2]," +
" document = target.ownerDocument || document," +
" window = document.defaultView || window;" +
"" +
"var input = document.createElement('INPUT');" +
"input.type = 'file';" +
"input.style.display = 'none';" +
"input.onchange = function () {" +
" var rect = target.getBoundingClientRect()," +
" x = rect.left + (offsetX || (rect.width >> 1))," +
" y = rect.top + (offsetY || (rect.height >> 1))," +
" dataTransfer = { files: this.files };" +
"" +
" ['dragenter', 'dragover', 'drop'].forEach(function (name) {" +
" var evt = document.createEvent('MouseEvent');" +
" evt.initMouseEvent(name, !0, !0, window, 0, 0, 0, x, y, !1, !1, !1, !1, 0, null);" +
" evt.dataTransfer = dataTransfer;" +
" target.dispatchEvent(evt);" +
" });" +
"" +
" setTimeout(function () { document.body.removeChild(input); }, 25);" +
"};" +
"document.body.appendChild(input);" +
"return input;";
WebElement input = (WebElement) jse.executeScript(JS_DROP_FILE, target, 0, 0);
input.sendKeys(filePath.getAbsoluteFile().toString());
waitFor(ExpectedConditions.stalenessOf(input));
}

Use AWT Robot class for performing drag and drop:
Robot robot=new Robot();
// drag
robot.mouseMove(x1, y1);
robot.mousePress(InputEvent.BUTTON1_MASK);
// drop
robot.mouseMove(x2, y2);
robot.mouseRelease(InputEvent.BUTTON1_MASK);

Related

Form suddenly unsignable for no obvious reason [duplicate]

below the code using one text field, two list boxes and one signature field. selecting an entry of listbox domicilation updates the entries of listbox legalForm. The newDateField currently servers as debug field, it contains the last listbox value that was updated. I would like to know why the result cannot be signed. It must be related to the javascript of the list boxes... Kindly help
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript;
import org.apache.pdfbox.pdmodel.interactive.action.PDAnnotationAdditionalActions;
import org.apache.pdfbox.pdmodel.interactive.action.PDFormFieldAdditionalActions;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream;
import org.apache.pdfbox.pdmodel.interactive.form.*;
import java.awt.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class AnaCreditForm {
public static void main(String[] args) {
System.out.println("Creating pdf docoument including signature field");
try {
// Create a new document with an empty page.
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
String javaScript = "var now = util.printd('yyyy-mm-dd', new Date());"
+ "var ndf = this.getField('newDateField');"
+ "ndf.value = now;"
// + "this.getField('signatureField').display=display.hidden;"
// + "var formReady = false;"
+ "var anacredit = { '-': [['-', '-']], "
+ " 'Luxembourg': [[ '-', '-'], ['LU01 Entreprise individuelle', 'LU01'],[ 'LU06 Société anonyme', 'LU06'] ,['LU14 Société civile','LU14']] , "
+ " 'Germany': [[ '-', '-'], ['DE201 Aktiengesellschaft', 'DE201'], ['DE602 Eingetragener Verein', 'DE602'], ['DE205 Investmentaktiengesellschaft', 'DE205']], "
+ " 'Greece': [[ '-', '-'], ['GR906 Εταιρία Περιορισμένης Ευθύνης/Etería Periorisménis Euthínis', 'GR906'], ['GR912 Κοινοπραξία/Kinopraxia', 'GR912'], ['GR999 Λοιπά/Lipa', 'GR999']] };";
// Create an action as JavaScript action
PDActionJavaScript jsAction = new PDActionJavaScript();
jsAction.setAction(javaScript);
// Set the action to be executed when the document is opened
document.getDocumentCatalog().setOpenAction(jsAction);
// Adobe Acrobat uses Helvetica as a default font and
// stores that under the name '/Helv' in the resources dictionary
PDFont font = PDType1Font.HELVETICA;
PDResources resources = new PDResources();
resources.put(COSName.getPDFName("Helv"), font);
PDDocumentCatalog pdCatalog = document.getDocumentCatalog();
PDAcroForm pdAcroForm = new PDAcroForm(document);
pdCatalog.setAcroForm(pdAcroForm);
pdAcroForm.setDefaultResources(resources);
String defaultAppearanceString = "/Helv 0 Tf 0 g";
pdAcroForm.setDefaultAppearance(defaultAppearanceString);
PDTextField newDateField = new PDTextField(pdAcroForm);
newDateField.setPartialName("newDateField");
defaultAppearanceString = "/Helv 12 Tf 0 g";
newDateField.setDefaultAppearance(defaultAppearanceString);
pdAcroForm.getFields().add(newDateField);
PDAnnotationWidget widget = newDateField.getWidgets().get(0);
PDRectangle rect = new PDRectangle(50, 450, 500, 15);
widget.setRectangle(rect);
widget.setPage(page);
// make sure the annotation is visible on screen and paper
widget.setPrinted(true);
// Add the annotation to the page
page.getAnnotations().add(widget);
//newDateField.setValue("value in newly created text field");
//textBox.setActions(fieldActions);
PDListBox domicilation = new PDListBox(pdAcroForm);
domicilation.setPartialName("domicilation");
List<String> displayList = Arrays.asList("-", "Germany", "Luxembourg", "Greece");
List<String> exportList = Arrays.asList("-", "Germany", "Luxembourg", "Greece");
domicilation.setOptions(exportList, displayList);
defaultAppearanceString = "/Helv 12 Tf 0 g";
domicilation.setDefaultAppearance(defaultAppearanceString);
pdAcroForm.getFields().add(domicilation);
String jsListBox0 =
"var f = this.getField('domicilation');"
+ "var r = this.getField('legalForm');"
+ " console.println('domicilation ' + f.value + 'legalForm' + r.value);"
+ "f.setAction('Keystroke', 'fft();');"
+ "function fft() { if (event.willCommit)"
+ "{ console.println('domiciliation' + event.change + ' ' + event.value); "
+ "r.setItems( anacredit[event.value] );"
+ "f.value=event.value) ; ndf.value= event.value;"
+ " }}";
// + "r.value='-'; formReady=false; }}";
PDFormFieldAdditionalActions fieldActions = new PDFormFieldAdditionalActions();
PDActionJavaScript jsKeystrokeAction = new PDActionJavaScript();
//jsKeystrokeAction.setAction("app.alert(\"On 'keystroke' action\")");
jsKeystrokeAction.setAction(jsListBox0);
fieldActions.setK(jsKeystrokeAction);
domicilation.setActions(fieldActions);
PDAnnotationWidget widget2 = domicilation.getWidgets().get(0);
PDRectangle rect2 = new PDRectangle(50, 380, 500, 50);
widget2.setRectangle(rect2);
widget2.setPage(page);
// make sure the annotation is visible on screen and paper
widget2.setPrinted(true);
//PDAnnotationAdditionalActions annotationActions = new PDAnnotationAdditionalActions();
// Add the annotation to the page
page.getAnnotations().add(widget2);
domicilation.setValue("-");
PDListBox legalForm = new PDListBox(pdAcroForm);
legalForm.setPartialName("legalForm");
List<String> displayList2 = Arrays.asList("-");
List<String> exportList2 = Arrays.asList(" ");
legalForm.setOptions(exportList2, displayList2);
defaultAppearanceString = "/Helv 12 Tf 0 g";
legalForm.setDefaultAppearance(defaultAppearanceString);
pdAcroForm.getFields().add(legalForm);
PDAnnotationWidget widget3 = legalForm.getWidgets().get(0);
PDRectangle rect3 = new PDRectangle(50, 310, 500, 50);
widget3.setRectangle(rect3);
widget3.setPage(page);
// make sure the annotation is visible on screen and paper
widget3.setPrinted(true);
String jsListBox2 = "var lb = this.getField('legalForm'); "
+ "console.println('in legalForm action ' + lb.value);"
+ "lb.setAction('Keystroke', 'fft2();');"
+ "function fft2() { if (event.willCommit)"
+ "{ console.println('in legalForm action ' + event.change + ' ' + event.value);"
+ "lb.value=event.value; ndf.value= event.value;}}";
// + "console.println(formReady);"
// + "lb.setAction('Keystroke', 'flb();');"
// + "function flb() { if (event.willCommit)"
// + "{ console.println('in listbox action'); console.println(event.value); "
// + "if (lb.value == '-') formReady= false; else formReady=true; "
// + "if (formReady) this.getField('signatureField').display=display.visible; "
// + "else this.getField('signatureField').display=display.hidden; }}" +
// + " lb.value=event.value; ndf.value=event.value; }}" ;
// "f2.setAction('Keystroke', 'fft2();');function fft2() { if (!event.willCommit) { console.println(event.change); r2.value = event.change; }}";
PDFormFieldAdditionalActions fieldActions2 = new PDFormFieldAdditionalActions(); // usable only for .setK, not for .setU
//PDAnnotationAdditionalActions annotationActions = new PDAnnotationAdditionalActions();
PDActionJavaScript jsKeyStrokeAction = new PDActionJavaScript();
//jsKeystrokeAction.setAction("app.alert(\"On 'keystroke' action\")");
jsKeyStrokeAction.setAction(jsListBox2);
fieldActions2.setK(jsKeyStrokeAction);
legalForm.setActions(fieldActions2);
//widget3.setActions(annotationActions);*/
//PDAnnotationAdditionalActions annotationActions = new PDAnnotationAdditionalActions();
PDFormFieldAdditionalActions listboxAction2 = new PDFormFieldAdditionalActions();
// Add the annotation to the page
page.getAnnotations().add(widget3);
legalForm.setValue("-");
PDRectangle rect4 = new PDRectangle(50, 150, 200, 50);
PDAppearanceDictionary appearanceDictionary = new PDAppearanceDictionary();
PDAppearanceStream appearanceStream = new PDAppearanceStream(document);
appearanceStream.setBBox(rect4.createRetranslatedRectangle());
appearanceStream.setResources(resources);
appearanceDictionary.setNormalAppearance(appearanceStream);
PDPageContentStream contentStream = new PDPageContentStream(document, appearanceStream);
contentStream.setStrokingColor(Color.BLACK);
contentStream.setNonStrokingColor(Color.LIGHT_GRAY);
contentStream.setLineWidth(2);
contentStream.addRect(0, 0, rect4.getWidth(), rect4.getHeight());
contentStream.fill();
contentStream.moveTo(1 * rect4.getHeight() / 4, 1 * rect4.getHeight() / 4);
contentStream.lineTo(2 * rect4.getHeight() / 4, 3 * rect4.getHeight() / 4);
contentStream.moveTo(1 * rect4.getHeight() / 4, 3 * rect4.getHeight() / 4);
contentStream.lineTo(2 * rect4.getHeight() / 4, 1 * rect4.getHeight() / 4);
contentStream.moveTo(3 * rect4.getHeight() / 4, 1 * rect4.getHeight() / 4);
contentStream.lineTo(rect4.getWidth() - rect4.getHeight() / 4, 1 * rect4.getHeight() / 4);
contentStream.stroke();
contentStream.setNonStrokingColor(Color.DARK_GRAY);
contentStream.beginText();
contentStream.setFont(font, rect4.getHeight() / 5);
contentStream.newLineAtOffset(3 * rect4.getHeight() / 4, -font.getBoundingBox().getLowerLeftY() * rect4.getHeight() / 5000);
contentStream.showText("Customer");
contentStream.endText();
contentStream.close();
PDSignatureField signatureField = new PDSignatureField(pdAcroForm);
signatureField.setPartialName("signatureField");
PDAnnotationWidget widget4 = signatureField.getWidgets().get(0);
widget4.setAppearance(appearanceDictionary);
widget4.setRectangle(rect4);
widget4.setPage(page);
page.getAnnotations().add(widget4);
pdAcroForm.getFields().add(signatureField);
document.save("anacreditForm.pdf");
for (PDField pdField : pdAcroForm.getFields()) {
System.out.println(pdField.getFullyQualifiedName() + " " + pdField.getFieldType() + " " + pdField.getValueAsString());
}
document.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Adobe Reader apparently does reject signing attempts for documents in which by means of JavaScript an event handler has been changed.
Your code actually is an example why that is good: As soon as the original domicilation event handler is executed, it is set to 'fft();' but the code of fft() is only in the Adobe Reader's memory, not in the PDF anymore. Thus, a thereafter signed version of the PDF would behave differently as it would be missing that code.
As an aside, there is one unwanted closing round bracket in
+ "f.value=event.value) ; ndf.value= event.value;"
in jsListBox0.

YouTube iFrame API Not Working Properly In JavaFX WebView?

So recently I've been working on making an audioplayer that can play playlists made by the user (mp3 files, wav files etc.) but that can also play YouTube playlists. In order to play YouTube videos I use JavaFX's WebView in combination with Google's YouTube iFrame API.
In order to do so, I use the following code;
#Override
public void loadList(String list)
{
html = "<!DOCTYPE html>\n" +
"<html>\n" +
" <body>\n" +
"<iframe id=\"player\" width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/videoseries?list=PL7aXwAD1wk5kdkuQOFBHY63R9Bhki7rCg&enablejsapi=1\" frameborder=\"0\" gesture=\"media\" allowfullscreen></iframe>\n" +
"\n" +
"<script type=\"text/javascript\">\n" +
" var tag = document.createElement('script');\n" +
" tag.id = 'iframe-demo';\n" +
" tag.src = 'https://www.youtube.com/iframe_api';\n" +
" var firstScriptTag = document.getElementsByTagName('script')[0];\n" +
" var volume = 100;\n" +
" firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);\n" +
"\n" +
" var player;\n" +
" function onYouTubeIframeAPIReady() {\n" +
" player = new YT.Player('player', {\n" +
" events: {\n" +
" 'onReady': onPlayerReady,\n" +
" 'onError' : handleError,\n" +
" 'onStateChange' : stateChange,\n" +
" 'onApiChange' : stateChange\n" +
" }\n" +
" });\n" +
" }\n" +
" function onPlayerReady(event) {\n" +
" event.target.playVideo();\n" +
" //event.target.nextVideo();\n" +
" }\n" +
" \n" +
" function handleError(event)\n" +
" {\n" +
" console.log(event);\n" +
" player.nextVideo();\n" +
" }\n" +
" \n" +
" function setVolume(vol)\n" +
" {\n" +
" volume = vol;\n" +
" player.setVolume(vol);\n" +
" }\n" +
" \n" +
" function stateChange(event)\n" +
" {\n" +
" // player.setVolume(volume + 1);\n" +
" // player.setVolume(volume - 1);\n" +
" // player.setVolume(volume);\n" +
" }\n" +
"</script>\n" +
" </body>\n" +
"</html>";
jfxPanel = new JFXPanel();
Platform.runLater ( new Runnable () {
#Override
public void run () {
player = new WebView();
player.getEngine().setJavaScriptEnabled(true);
player.getEngine().loadContent(html);
jfxPanel.setScene(new Scene(player));
JFrame f = new JFrame();
f.add(jfxPanel);
f.setSize(1280,720);
f.setVisible(true);
}
} );
GraphicalInterface.uptodate = false;
}
The issue I am having is this:
Once I use the API's 'player.setVolume(vol)' functionality, it correctly changes the volume for the video it is currently playing. When a new video loads, the videoplayer still shows the volume is the same as it had previously been set to, but it outputs too high of a volume;
first video, lowered volume;
second video, volumeslider is lowered, volume itself isn't;
I only have this issue when using JavaFX's WebView. When I paste the HTML code in a .html file and run it that way, the audio is correctly lowered (and not just the audio slider).
Does anyone know why this could be happening and how I can fix this? Does it have to do with the WebView's settings?
Note:
In the JavaScript I have commented out a small piece of code. This code is a small temporary workaround that "re-setd" the audio to where it should be when a new video is loaded. This solution is not ideal though.

Restar Loader can't update gridview

I've got two SQL tables inner join with content provider query builder. My loader shows the first in a gridview like a charm. The second table has been created to show a favorite list and so it has primary key, foreign key and another column. By the way when I try to retrieve value from this one I get null. Some suggestions, please!
I have this in content provider:
static {
sMovieByFavoriteQueryBuilder = new SQLiteQueryBuilder();
sMovieByFavoriteQueryBuilder.setTables(
MoviesContract.FavoriteEntry.TABLE_NAME + " INNER JOIN " +
MoviesContract.MovieEntry.TABLE_NAME +
" ON " + MoviesContract.FavoriteEntry.TABLE_NAME +
"." + MoviesContract.FavoriteEntry.COLUMN_FAVORITE_KEY +
" = " + MoviesContract.MovieEntry.TABLE_NAME +
"." + MoviesContract.MovieEntry._ID);
}
private static final String sFavoriteSelection =
MoviesContract.FavoriteEntry.TABLE_NAME +
"." + MoviesContract.FavoriteEntry.COLUMN_MOVIE_ID + " = ? AND " +
MoviesContract.FavoriteEntry.COLUMN_MOVIE_POSTER + " = ? AND " +
MoviesContract.MovieEntry.COLUMN_RELEASE_DATE + " = ? AND " +
MoviesContract.MovieEntry.COLUMN_MOVIE_POSTER + " = ? AND " +
MoviesContract.MovieEntry.COLUMN_ORIGINAL_TITLE + " = ? AND " +
MoviesContract.MovieEntry.COLUMN_SYNOSIS + " = ? AND " +
MoviesContract.MovieEntry.COLUMN_USER_RATING + " = ? ";
I call this method by uri from fragment:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
getLoaderManager().initLoader(MOVIES_LOADER, null, this);
getLoaderManager().initLoader(FAVORITE_LOADER, null, this);
super.onActivityCreated(savedInstanceState);
}
void onSortChanged() {
System.out.println("onSortChanged: true");
updateMovies();
getLoaderManager().restartLoader(MOVIES_LOADER, null, this);
System.out.println("LoaderManager: " + getLoaderManager().restartLoader(MOVIES_LOADER, null, this));
}
void onFavorite() {
getLoaderManager().restartLoader(FAVORITE_LOADER, null, this);
mMoviesAdapter.setSelectedIndex(mPosition);
mMoviesAdapter.notifyDataSetChanged();
}
private void updateMovies() {
PopularMoviesSyncAdapter.syncImmediately(getActivity());
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (MainActivity.mFavorite == true) {
String sortOrder = MoviesContract.FavoriteEntry.COLUMN_FAVORITE_KEY;
String sortSetting = Utility.getPreferredSort(getActivity());
Uri movieFavoriteUri = MoviesContract.MovieEntry.buildMovieWithSortDate(sortSetting, System.currentTimeMillis());
System.out.println("movieFavoriteUri: " + movieFavoriteUri);
cursorFav = new CursorLoader(getActivity(),
movieFavoriteUri,
FAVORITE_COLUMNS,
null,
null,
sortOrder);
return cursorFav;
} else {
String sortOrder = MoviesContract.MovieEntry.COLUMN_DATE;
String sortSetting = Utility.getPreferredSort(getActivity());
System.out.println("sortSetting: " + sortSetting);
Uri movieForSortUri = MoviesContract.MovieEntry.buildMovieSort(sortSetting);
System.out.println("movieForSortUri: " + movieForSortUri);
cursorMov = new CursorLoader(getActivity(),
movieForSortUri,
MOVIES_COLUMNS,
null,
null,
sortOrder);
return cursorMov;
}
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mMoviesAdapter.swapCursor(data);
if (mPosition != GridView.INVALID_POSITION) {
// If we don't need to restart the loader, and there's a desired position to restore
// to, do so now.
mGridView.smoothScrollToPosition(mPosition);
}
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
mMoviesAdapter.swapCursor(null);
}
Instead of:
sMovieByFavoriteQueryBuilder.setTables(
MoviesContract.FavoriteEntry.TABLE_NAME + " INNER JOIN " +
MoviesContract.MovieEntry.TABLE_NAME +
" ON " + MoviesContract.FavoriteEntry.TABLE_NAME +
"." + MoviesContract.FavoriteEntry.COLUMN_FAVORITE_KEY +
" = " + MoviesContract.MovieEntry.TABLE_NAME +
"." + MoviesContract.MovieEntry._ID);
Create a string so you can debug it easy.
strTables = MoviesContract.FavoriteEntry.TABLE_NAME + " INNER JOIN " +
MoviesContract.MovieEntry.TABLE_NAME +
" ON " + MoviesContract.FavoriteEntry.TABLE_NAME +
"." + MoviesContract.FavoriteEntry.COLUMN_FAVORITE_KEY +
" = " + MoviesContract.MovieEntry.TABLE_NAME +
"." + MoviesContract.MovieEntry._ID);
sMovieByFavoriteQueryBuilder.setTables(strTables);
Then try to run that join direct on db. My guess is the query have some issues.
I checked and the query works good. But my method can't return anything
private Cursor getMovieByFavorite(Uri uri, String[] projection, String movieId) {
String sortSetting = MoviesContract.MovieEntry.getFavoriteFromUri(uri);
long date = MoviesContract.MovieEntry.getDateFromUri(uri);
String[] selectionArgs;
String selection;
selection = sFavoriteSelection;
selectionArgs = new String[]{sortSetting};
return sMovieByFavoriteQueryBuilder.query(mOpenHelper.getReadableDatabase(),
projection,
selection,
null,
null,
null,
movieId
);
}

Execute Javascript using Selenium or HtmlUnit

I need small help.
I want to open some page using Java and Selenium or HtmlUnit, and after opening this page execute url like Ajax and get to String the response.
Let say, a want to open http://www.somepage.com , when driver is still on this page, execute GET http://www.somepage.com/myAjax/xyz , which should return JSON.
Then i want to get the JSON response and do something with it.
Could you help me, how to do it ?
Best regards
To inject your own javascript, you can do something like:
new WebConnectionWrapper(webClient) {
public WebResponse getResponse(WebRequest request) throws IOException {
WebResponse response = super.getResponse(request);
if (request.getUrl().toExternalForm().contains("my_url")) {
String content = response.getContentAsString("UTF-8");
// inject the below to the 'content'
String tobeInjected = ""
+ "<script>\n"
+ "var myOwnVariable;\n"
+ "var xmlhttp;\n"
+ "if (window.XMLHttpRequest) {\n"
+ " xmlhttp=new XMLHttpRequest();\n"
+ "}\n"
+ "else {\n"
+ " xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');\n"
+ "}\n"
+ "\n"
+ "xmlhttp.onreadystatechange=function() {\n"
+ " if (xmlhttp.readyState==4 && xmlhttp.status==200) {\n"
+ " myOwnVariable = xmlhttp.responseText;\n"
+ " }\n"
+ "}\n"
+ "\n"
+ "xmlhttp.open('GET', 'http://www.somepage.com/myAjax/xyz', true);\n"
+ "xmlhttp.send();\n"
+ "</script>";
WebResponseData data = new WebResponseData(content.getBytes("UTF-8"),
response.getStatusCode(), response.getStatusMessage(), response.getResponseHeaders());
response = new WebResponse(data, request, response.getLoadTime());
}
return response;
}
};
To retrieve the value of the javascript variable:
webClient.waitForBackgroundJavaScript(5_000);
String value = htmlPage.executeJavaScript("myOwnVariable").toString();

Failed to parse resource-request java.net.URISyntaxException: Expected scheme name at index 0: ://

I try to make yarn application master and runs application on hadoop 2. These are sources
=== Client.java
public class MyClient {
Configuration conf = new YarnConfiguration();
public void run(String[] args) throws Exception {
final int n = 1;
final Path jarPath = new Path("./");
YarnConfiguration conf = new YarnConfiguration();
YarnClient yarnClient = YarnClient.createYarnClient();
yarnClient.init(conf);
yarnClient.start();
YarnClientApplication app = yarnClient.createApplication();
ContainerLaunchContext amContainer = Records.newRecord(ContainerLaunchContext.class);
amContainer.setCommands(
Collections.singletonList(
"$JAVA_HOME/bin/java" + " -Xmx256M" + " com.aaa.yarn.MyApplicationMaster" + " " + jarPath +
" " + String.valueOf(n) +
" 1>" + ApplicationConstants.LOG_DIR_EXPANSION_VAR + "/stdout" +
" 2>" + ApplicationConstants.LOG_DIR_EXPANSION_VAR + "/stderr"
)
);
LocalResource appMasterJar = Records.newRecord(LocalResource.class);
setupAppMasterJar(jarPath, appMasterJar);
amContainer.setLocalResources(Collections.singletonMap("YarnHelloWorld.jar", appMasterJar));
Map<String, String> appMasterEnv = new HashMap<String, String>();
setupAppMasterEnv(appMasterEnv);
amContainer.setEnvironment(appMasterEnv);
Resource capability = Records.newRecord(Resource.class);
capability.setMemory(256);
capability.setVirtualCores(1);
ApplicationSubmissionContext appContext = app.getApplicationSubmissionContext();
appContext.setApplicationName("Yarn_Hello_World"); // application name
appContext.setAMContainerSpec(amContainer);
appContext.setResource(capability);
appContext.setQueue("default"); // queue
ApplicationId appId = appContext.getApplicationId();
System.out.println("Submitting application " + appId);
yarnClient.submitApplication(appContext);
ApplicationReport appReport = yarnClient.getApplicationReport(appId);
YarnApplicationState appState = appReport.getYarnApplicationState();
while (appState != YarnApplicationState.FINISHED && appState != YarnApplicationState.KILLED &&
appState != YarnApplicationState.FAILED) {
Thread.sleep(100);
appReport = yarnClient.getApplicationReport(appId);
appState = appReport.getYarnApplicationState();
}
System.out.println("Application " + appId + " finished with" + " state " + appState + " at " + appReport.getFinishTime());
}
private void setupAppMasterJar(Path jarPath, LocalResource appMasterJar) throws IOException {
FileStatus jarStat = FileSystem.get(conf).getFileStatus(jarPath);
appMasterJar.setResource(ConverterUtils.getYarnUrlFromPath(jarPath));
appMasterJar.setSize(jarStat.getLen());
appMasterJar.setTimestamp(jarStat.getModificationTime());
appMasterJar.setType(LocalResourceType.FILE);
appMasterJar.setVisibility(LocalResourceVisibility.PUBLIC);
}
private void setupAppMasterEnv(Map<String, String> appMasterEnv) {
StringBuilder classPathEnv = new StringBuilder(Environment.CLASSPATH.$$())
.append(ApplicationConstants.CLASS_PATH_SEPARATOR).append("./*");
for (String c : conf.getStrings(YarnConfiguration.YARN_APPLICATION_CLASSPATH,
YarnConfiguration.DEFAULT_YARN_CROSS_PLATFORM_APPLICATION_CLASSPATH)) {
classPathEnv.append(ApplicationConstants.CLASS_PATH_SEPARATOR);
classPathEnv.append(c.trim());
}
appMasterEnv.put("CLASSPATH", classPathEnv.toString());
}
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
MyClient c = new MyClient();
c.run(args);
}
}
=== My Application Master
public class MyApplicationMaster {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
final int n = 1;
Configuration conf = new YarnConfiguration();
AMRMClient<ContainerRequest> rmClient = AMRMClient.createAMRMClient();
rmClient.init(conf);
rmClient.start();
NMClient nmClient = NMClient.createNMClient();
nmClient.init(conf);
nmClient.start();
System.out.println("registerApplicationMaster 0");
rmClient.registerApplicationMaster("", 0, "");
System.out.println("registerApplicationMaster 1");
Priority priority = Records.newRecord(Priority.class);
priority.setPriority(0);
Resource capability = Records.newRecord(Resource.class);
capability.setMemory(128);
capability.setVirtualCores(1);
for (int i = 0; i < n; ++i) {
ContainerRequest containerAsk = new ContainerRequest(capability, null, null, priority);
System.out.println("Making res-req " + i);
rmClient.addContainerRequest(containerAsk);
}
int responseId = 0;
int completedContainers = 0;
while (completedContainers < n) {
AllocateResponse response = rmClient.allocate(responseId++);
for (Container container : response.getAllocatedContainers()) {
ContainerLaunchContext ctx = Records.newRecord(ContainerLaunchContext.class);
ctx.setCommands(
Collections.singletonList(
"$JAVA_HOME/bin/java" + " -Xmx256M" + " com.aaa.yarn.YarnHelloWorld" +
" 1>" + ApplicationConstants.LOG_DIR_EXPANSION_VAR + "/stdout" +
" 2>" + ApplicationConstants.LOG_DIR_EXPANSION_VAR + "/stderr"
));
System.out.println("Launching container " + container.getId());
nmClient.startContainer(container, ctx);
}
for (ContainerStatus status : response.getCompletedContainersStatuses()) {
++completedContainers;
System.out.println("Completed container " + status.getContainerId());
}
Thread.sleep(100);
}
rmClient.unregisterApplicationMaster(
FinalApplicationStatus.SUCCEEDED, "", "");
}
}
But deployment is failed. In nodemanager log the following exception is thrown
WARN org.apache.hadoop.yarn.server.nodemanager.containermanager.container.Container: Failed to parse resource-request
java.net.URISyntaxException: Expected scheme name at index 0: ://
at java.net.URI$Parser.fail(URI.java:2848)
at java.net.URI$Parser.failExpecting(URI.java:2854)
at java.net.URI$Parser.parse(URI.java:3046)
at java.net.URI.<init>(URI.java:746)
at org.apache.hadoop.yarn.util.ConverterUtils.getPathFromYarnURL(ConverterUtils.java:80)
at org.apache.hadoop.yarn.server.nodemanager.containermanager.localizer.LocalResourceRequest.<init>(LocalResourceRequest.java:46)
at org.apache.hadoop.yarn.server.nodemanager.containermanager.container.ContainerImpl$RequestResourcesTransition.transition(ContainerImpl.java:632)
at org.apache.hadoop.yarn.server.nodemanager.containermanager.container.ContainerImpl$RequestResourcesTransition.transition(ContainerImpl.java:590)
at org.apache.hadoop.yarn.state.StateMachineFactory$MultipleInternalArc.doTransition(StateMachineFactory.java:385)
at org.apache.hadoop.yarn.state.StateMachineFactory.doTransition(StateMachineFactory.java:302)
at org.apache.hadoop.yarn.state.StateMachineFactory.access$300(StateMachineFactory.java:46)
at org.apache.hadoop.yarn.state.StateMachineFactory$InternalStateMachine.doTransition(StateMachineFactory.java:448)
at org.apache.hadoop.yarn.server.nodemanager.containermanager.container.ContainerImpl.handle(ContainerImpl.java:992)
at org.apache.hadoop.yarn.server.nodemanager.containermanager.container.ContainerImpl.handle(ContainerImpl.java:76)
at org.apache.hadoop.yarn.server.nodemanager.containermanager.ContainerManagerImpl$ContainerEventDispatcher.handle(ContainerManagerImpl.java:1065)
at org.apache.hadoop.yarn.server.nodemanager.containermanager.ContainerManagerImpl$ContainerEventDispatcher.handle(ContainerManagerImpl.java:1058)
at org.apache.hadoop.yarn.event.AsyncDispatcher.dispatch(AsyncDispatcher.java:173)
at org.apache.hadoop.yarn.event.AsyncDispatcher$1.run(AsyncDispatcher.java:106)
at java.lang.Thread.run(Thread.java:745)
I think there is some problem on this line
amContainer.setCommands(
Collections.singletonList(
"Environment.JAVA_HOME.$$()" + "/bin/java" + " -Xmx256M" + " com.aaa.yarn.MyApplicationMaster" + " " + jarPath +
" " + String.valueOf(n) +
" 1>" + ApplicationConstants.LOG_DIR_EXPANSION_VAR + "/stdout" +
" 2>" + ApplicationConstants.LOG_DIR_EXPANSION_VAR + "/stderr"
)
);
But I have no idea.