Created
August 5, 2015 13:11
-
-
Save suntereichner/786ac329c0f3897138cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.borland.silktest.jtf.Control; | |
import com.borland.silktest.jtf.Desktop; | |
import com.borland.silktest.jtf.Utils; | |
import org.junit.Assert; | |
import org.junit.Before; | |
import com.borland.silktest.jtf.BrowserBaseState; | |
import org.junit.Test; | |
import com.borland.silktest.jtf.common.CommonOptions; | |
import com.borland.silktest.jtf.common.types.MouseButton; | |
import com.borland.silktest.jtf.common.types.Point; | |
import com.borland.silktest.jtf.common.types.Rect; | |
import com.borland.silktest.jtf.xbrowser.BrowserApplication; | |
import com.borland.silktest.jtf.xbrowser.DomButton; | |
import com.borland.silktest.jtf.xbrowser.DomElement; | |
public class IE11Download { | |
private Desktop desktop = new Desktop(); | |
@Before | |
public void baseState() { | |
// Go to web page 'http://demo.borland.com/testsite/download_testpage.php' | |
BrowserBaseState baseState = new BrowserBaseState(); | |
baseState.execute(desktop); | |
} | |
private void waitForTextExists(String locator, String text, int timeout) { | |
final int retryExistsTimeout = 1000; | |
final int initialRetryCount = timeout / retryExistsTimeout; | |
int retryCount = initialRetryCount; | |
// Call find repeatedly in case position and size of control changes. | |
while ((retryCount > 0) && !desktop.<Control>find(locator).textExists(text, 1, null, retryExistsTimeout)) | |
{ | |
--retryCount; | |
System.out.println(String.format("Retry %1$d wait for '%2$s'", initialRetryCount - retryCount, text)); | |
} | |
if (retryCount == 0) | |
{ | |
Assert.fail(String.format("Failed waiting for text '%1$s' in locator '%2$s'", text, locator)); | |
} | |
} | |
@Test | |
public void downloadFile() { | |
desktop.<DomElement> find("//BrowserApplication//BrowserWindow//LI[@textContents='attachment + filename']").click(MouseButton.LEFT); | |
String fileToDownload = "Data50MB.dat"; | |
String downloadFileSelectionLocator = String.format("//BrowserApplication//BrowserWindow//TR[@filename='%1$s']/TD[1]", fileToDownload); | |
desktop.<DomElement> find(downloadFileSelectionLocator).click(MouseButton.LEFT); | |
final int SYNC_MODE_HTML = 1; | |
// Change to HTML sync for clicking on download button. | |
// With AJAX sync the click would not return early enough to press the "Save" button. | |
Object oldSyncMode = desktop.getOption(CommonOptions.OPT_XBROWSER_SYNC_MODE); | |
desktop.setOption(CommonOptions.OPT_XBROWSER_SYNC_MODE, SYNC_MODE_HTML); | |
System.out.println("Click DOWNLOAD"); | |
desktop.<DomButton> find("//BrowserApplication//BrowserWindow//BUTTON[@id='downloadButton']").click(MouseButton.LEFT); | |
desktop.setOption(CommonOptions.OPT_XBROWSER_SYNC_MODE, oldSyncMode); | |
System.out.println("Download bar: Wait for 'Save'"); | |
waitForTextExists("//BrowserApplication//Control[@windowClassName='DirectUIHWND']", "Save", 40 * 1000); | |
Utils.sleep(1000); // Do not click too early. Click might be swallowed by IE. | |
System.out.println("Download bar: Click 'Save'"); | |
desktop.<Control> find("//BrowserApplication//Control[@windowClassName='DirectUIHWND']").textClick("Save"); | |
// Explicitly waiting for download | |
waitForTextExists("//BrowserApplication//Control[@windowClassName='DirectUIHWND']", "download has completed", 60 * 1000); | |
// find again because position/size may have changed | |
Control downloadBar = desktop.<Control> find("//BrowserApplication//Control[@windowClassName='DirectUIHWND']"); | |
Rect downloadBarRect = downloadBar.getRect(); | |
System.out.println("Download Bar: " + downloadBarRect.toString()); | |
System.out.println("Download bar: Close"); | |
Point closeButtonPos = new Point(downloadBarRect.getWidth() - 17, downloadBarRect.getHeight() - 19); | |
downloadBar.click(MouseButton.LEFT, closeButtonPos); | |
desktop.<BrowserApplication> find("WebBrowser").close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment