Skip to content

Commit

Permalink
#133: open a HTML snippet with WebDriver
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Pfotenhauer committed Aug 20, 2020
1 parent d42b51a commit 689636d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/main/java/com/xceptance/neodymium/util/SelenideAddons.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.xceptance.neodymium.util;

import static com.codeborne.selenide.Selenide.$$;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.Selenide.sleep;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -440,8 +445,34 @@ public static void dragAndDropUntilCondition(SelenideElement elementToMove, Sele
});
}
dragAndDrop(elementToMove, horizontalMovement, verticalMovement);
Selenide.sleep(pauseBetweenMovements);
sleep(pauseBetweenMovements);
counter++;
}
}

/**
* Open the supplied HTML content with the current web driver.
*
* @param htmlContent
* a String containing the HTML that should be opened in the current web driver
*/
public static void openHtmlContentWithCurrentWebDriver(String htmlContent)
{
File tempHtmlContentFile = null;
try
{
tempHtmlContentFile = File.createTempFile("htmlContent", ".html", new File("./target/"));
tempHtmlContentFile.deleteOnExit();

FileWriter fileWriter;
fileWriter = new FileWriter(tempHtmlContentFile);

This comment has been minimized.

Copy link
@h-arlt

h-arlt Aug 24, 2020

Contributor

Please use try-with-resources to ensure that the writer is always closed. Alternatively, use FileUtils provided by Apache Commons library.

Another approach to open some HTML snippet in a browser is to encode the snippet to base64 and open it as data URI. This way you would avoid file I/O completely.

fileWriter.append(htmlContent);
fileWriter.close();
}
catch (final IOException e)
{
e.printStackTrace();
}
open("file://" + tempHtmlContentFile.getAbsolutePath());
}
}
10 changes: 10 additions & 0 deletions src/test/java/com/xceptance/neodymium/util/SelenideAddonsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -537,4 +537,14 @@ private void leftHorizontalDragAndDropUntilAttribute(SelenideElement elementToMo
SelenideAddons.dragAndDropUntilCondition(elementToMove, elementToCheck, horizontalMovement, 0, 3000, 10,
Condition.attribute(sliderValueAttributeName, moveUntil));
}

@Test
public void testOpenHtmlContentWithCurrentWebDriver()
{
final String text = "Hi\n\nHow are you?)\n\nBye";
final String textHtml = "<div dir=\"auto\">Hi<div dir=\"auto\"><br></div><div dir=\"auto\">How are you?)</div><div dir=\"auto\"><br></div><div dir=\"auto\">Bye</div></div>";

SelenideAddons.openHtmlContentWithCurrentWebDriver(textHtml);
Assert.assertEquals(text, $("body").getText());
}
}

0 comments on commit 689636d

Please sign in to comment.