How to verify data in PDF that opened in next tab in chrome. Can any body help on this.
You can switch to the second tab like this:
String currentWindow = driver.getWindowHandle();
(new WebDriverWait(driver, 20)).until(ExpectedConditions.numberOfWindowsToBe(2));
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(currentWindow)) {
driver.switchTo().window(handle);
}
}
Related
I need to implement a carousel to show images/video/pdf/ppt files. I used Tab component to achieve it. But it's not working as expected. The first tab displays image correctly but at the same time pdf file gets opened which is there on the third tab. Second tab video does not play. I tried calling these events on tab selection but still not working. My code as below.
Form hi = new Form("Swipe Tabs", new LayeredLayout());
Tabs t = new Tabs();
t.hideTabs();
container1 = BoxLayout.encloseY();
container2 = BoxLayout.encloseY();
container3 = BoxLayout.encloseY();
InputStream is = null;
ImageViewer iv = null;
try{
is = Display.getInstance().getResourceAsStream(getClass(), "/Img1.png");
iv = new ImageViewer(Image.createImage(is));
}catch(Exception exc){
exc.printStackTrace();
}
container1 = BoxLayout.encloseY(iv);
FileSystemStorage fs = FileSystemStorage.getInstance();
fs.mkdir(fs.getAppHomePath());
String fileName = fs.getAppHomePath() + "test.mp4";
if(!fs.exists(fileName)) {
Util.downloadUrlToFile("http://localhost/app/test.mp4", fileName, true);
}
try{
Media video = MediaManager.createMedia(fileName, true);
video.setNativePlayerMode(true);
container2 = BoxLayout.encloseY(new MediaPlayer(video));
video.play();
}catch(Exception exc){
exc.printStackTrace();
}
fs = FileSystemStorage.getInstance();
fs.mkdir(fs.getAppHomePath());
final String fileName1 = fs.getAppHomePath() + "file1.pdf";
if(!fs.exists(fileName1)) {
Util.downloadUrlToFile("http://localhost/app/file1.pdf", fileName1, true);
}
container3 = BoxLayout.encloseY();
Display.getInstance().execute(fileName1);
t.addTab("Tab1", container1);
t.addTab("Tab2", container2);
t.addTab("Tab3", container3);
new ButtonGroup(firstTab, secondTab, thirdTab);
firstTab.setSelected(true);
Container tabsFlow = FlowLayout.encloseCenter(firstTab, secondTab, thirdTab);
hi.add(t);
hi.add(BorderLayout.south(tabsFlow));
hi.show();
Display.execute launches an external viewer to display something. It doesn't create a component you can embed. We don't support embedding a PDF as that isn't available on Android and is a bit flaky on iOS. If you only care about iOS you can use a BrowserComponent to show a PDF. You can use a button and invoke execute when the button is pressed to show a PDF.
Change this:
container2 = BoxLayout.encloseY(new MediaPlayer(video));
video.play();
To:
MediaPlayer mp = new MediaPlayer(video);
container2 = mp;
mp.setAutoplay(true);
mp.setLoop(true);
Finally, I would suggest using Log.e() not printStackTrace().
I'm passing an Id on click of a button(button is in 192.xxx.x.xxx\Profile\Details page) to angularJS controller file where it is calling an API
$scope.docView = function () {
Method.getbyId("api call",docId).then(function(response) {
if (response.data.message != null)
window.open('//'+response.data.message);
else
alert("File Not Found !");
}).catch(function (data) {
console.log("Unknown Error");
});
}
}
API is :
[Route("api call")]
[HttpGet]
public IHttpActionResult ViewDocument (Guid? docId)
{
/*background work*/
response.Message = filePath;
}
}
catch (Exception ex)
{
Utils.Write(ex);
}
return Ok(response);
}
In response.data.message, the path of the file(192.xxx.x.xxx\folder\filename) on the server is coming which will open the file via window.open(). But in URL, whole path of the folder is visible which will become security breach. Hence I want to 'display the file in a new View' in the same controller which will open the file in a new tab. The url should be like (192.xxx.x.xxx\Profile\Details? docid=xxxxxxxxxxxxxxxxx ).
What you are actually looking for, you can find with a quick search on Google:
"Download file from an ASP.NET Web API method".
For example
here and
here you'll find examples which implement what you are looking for.
EDIT:
HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(result);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue(System.Net.Mime.DispositionTypeNames.Inline)
{
FileName = "foo.pdf"
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
As derived from this and this Thread.
You would need to change the Content-Type according to the actual file type. Also if the file is actually viewable inside the Browser depends on wether the Browser is able to do so.
In my scenario, when I launch a URL, an authentication alert window opens up and I need to enter Username and Password from excel and on clicking on 'Log In' it'll show me the web homepage. I've implemented this using the below code snippet and it's working perfectly in IE11. But when I'm trying to run the same code in Chrome v60, it's not working. It's showing the authentication alert window but not performing any action on it. Screenshot attached. Can anyone suggest please.
Selenium WD version - 3.4.0
Code:
public static void loadApplicationURL(WebDriver driver) throws Exception
{
String SSPathl = null;
String URL = Generic_Functions.GetParameterFromInputSheet("URL");
try
{
driver.get(URL);
Thread.sleep(6000);
LoginApplication.isAlertPresent(driver);
}
}
public static boolean isAlertPresent(WebDriver driver) {
try {
driver.switchTo().alert();
UserAndPassword UP = new UserAndPassword(UserName,Password);
driver.switchTo().alert().authenticateUsing(UP);
Thread.sleep(8000);
return true;
}
catch (Exception e) {
return false;
}
}
For test browsers, have created a separate class. Find below the snippet used for IE and Chrome browsers.
For Chrome
System.setProperty("webdriver.chrome.driver",".\\Resources\\chromedriver.exe");
driver = new ChromeDriver()
In case of IE,
[System.setProperty("webdriver.ie.driver",".\\Resources\\IEDriverServer.exe");
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
ieCapabilities.setCapability("nativeEvents", true);
driver = new InternetExplorerDriver(ieCapabilities);][1]
I need to download a file from the server. The file is stored in the database. I have a cs controller that serves a file back to UI. The server GET call looks like this:
http://server/api/controllername/fileid/data
It does work when I run that link in the Browser - the file comes down and goes into the download area (Chrome). But when I send the same command from my Angualar code I dont see any file. The console reports that my request was successful (code 200), but I just dont see anything. Please let me know what code fragments to post to make it easier to help.
Thanks
Create a link to the resource, and don't handle it with ajax.
If you make the link open in a new tab, the tab will automatically close after it realises it was just opened to download a file in most modern browsers.
Try this code:
var a = document.createElement('a');
a.href = "http://server/api/controllername/fileid/data";
a.click();
You can compose the address concatenating variables and text.
The file probably downloads correctly as a byte[] to the calling it but that would be useless to the user - as was my problem.
In my case I needed to download a file with a complex set of parameters. This example JavaScript uses a post request and creates a form (and posts it) with any JavaScript object that you give it. This code may help if you simplified it:
private open(verb, url, data, target)
{
var form = document.createElement("form");
form.action = url;
form.method = verb;
form.target = target || "_self";
if (data) {
this.createFormParameters(form, "", data);
}
form.style.display = 'none';
document.body.appendChild(form);
form.submit();
}
private createFormParameters(form, key, value) {
// recursive algorithm to add parameters (including objects and arrays of objects) to a custom form
if (typeof value === "object") {
for (var item in value) {
if (Array.isArray(value[item])) {
for (var arrayItem in value[item]) {
this.createFormParameters(form, (item + "[" + arrayItem + "]."), value[item][arrayItem]);
}
continue;
}
var input = document.createElement("textarea");
input.name = key + item;
input.value = value[item];
form.appendChild(input);
}
}
else
{
var input = document.createElement("textarea");
input.name = key;
input.value = value;
form.appendChild(input);
}
}
Description:
I have multiple successive image downloads and saving in IsolatedStorage using HttpWebRequest.
After all image downloadsa are completed I need to navigate user to another page, where images are displayed in image controls from isolated storage.
Question:
How can I know when all the downloads are completed to run the navigation?
I tried to pass the redirect to the requests callback function (requestImage_BeginGetResponse()) in the last foreach loops iteration after saving the image,
but the images are different sizes and sometimes the last image downloads faster than previous, that results in redirect before all downloads are completed.
the code:
private HttpWebRequest request;
private void downloadDataFile()
{
...
foreach (Gallery image in gallery)
{
request = (HttpWebRequest)WebRequest.Create(image.url);
request.BeginGetResponse(new AsyncCallback(requestImage_BeginGetResponse), new object[] { request, image.name });
}
}, request);
}
private void requestImage_BeginGetResponse(IAsyncResult r)
{
object[] param = (object[])r.AsyncState;
HttpWebRequest httpRequest = (HttpWebRequest)param[0];
string filename = (string)param[1];
HttpWebResponse httpResoponse = (HttpWebResponse)httpRequest.EndGetResponse(r);
System.Net.HttpStatusCode status = httpResoponse.StatusCode;
if (status == System.Net.HttpStatusCode.OK)
{
Stream str = httpResoponse.GetResponseStream();
Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
{
saveImage(str, filename);
}));
}
}
You should prepare a int type variable to record your images that would be downloaded.Whenever a image is downloaded,make the variable minus 1 untill its value is 0,and notify the navigating operation.