Taking a screenshot of a remote desktop using Sikuli - remote-desktop

I have the following Java code which I am using to capture a screenshot:
import org.sikuli.script.Screen;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
public class Screenshot{
public static void main(String[] args) throws IOException
{
Screen screen = new Screen();
ImageIO.write(screen.capture(screen).getImage(), "png", new File("D:\\myScreen.png"));
}
}
I compile the piece of code using:
javac -classpath .;sikulixapi-2.0.4.jar Screenshot.java
and run it with:
java -classpath .;sikulixapi-2.0.4.jar Screenshot
I tried to run it remotely, using
psexec \\xx.xx.xxx.xxx -w "D:\Sikuli" java -classpath .;sikulixapi-2.0.4.jar Screenshot
The result is not the picture of the remote screen, but only a black background.
Is there any way to make this work?

To check wether you principally could get a shot of the remote screen this way, you can use what SikuliX uses internally: java.awt.Robot
try this:
import java.awt.*;
...
BufferedImage img = new Robot().createScreenCapture(new Rectangle(0, 0, 500, 500))
... and then your coding to store the image somewhere.
If the image is black, then you have a problem with the monitor setup on the remote system. It must be non-headless (real screen) and Robot must have access to an unlocked screen.
RaiMan from SikuliX

Related

Unable to identify the webelements on XHTML page

Unable to identify the webelements on XHTML page
In My project if I click on a link a PDF opens up in a new tab and I am trying to download the PDF by clicking on Download click.The Web Element is not getting identified as the download icon is on the XHTML page. I have tried replicating this using India Post website. Still unable to identify the element. Below is the code. Does anyone have a solution?
package com.demo.learn;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class handle {
public static void main (String [] args){
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "D://CAHCO Automation//OpteamixAutomation-master(1)//OpteamixAutomation-master//Automation_framework//OPTEAMIX//Drivers//chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://www.indiapost.gov.in/VAS/Pages/Form.aspx");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[#id='ctl00_SPWebPartManager1_g_29491a13_ee77_4e11_a5e4_e031cc13d245']/div[1]/table[1]/tbody/tr[2]/td[1]/a/img")).click();
Set<String> handles=driver.getWindowHandles();
String firsthandle = driver.getWindowHandle();
handles.remove(firsthandle);
String winhandle = handles.iterator().next();
if (winhandle!=firsthandle){
String secondWinHandle = winhandle;
driver.switchTo().window(secondWinHandle);
System.out.println("Switched");
boolean display = driver.findElement(By.xpath(".//*
[#id='download']")).isDisplayed();
if(display==true)
{
System.out.println("Displayed");
}
else
{
System.out.println("Not Displayed");
}
}
}
}
I guess what you are trying to do is clicking on PDF viewer in Chrome. The Chrome PDF viewer is a Chrome plugin so you cannot access it.
I recommend a workaround by adding an attribute to the link to open PDF. The attribute download will tell web browser to download it instead of open it in PDF Viewer. See my code below.
String script = "document.querySelector('#ctl00_SPWebPartManager1_g_29491a13_ee77_4e11_a5e4_e031cc13d245>div>table>tbody>tr>td>a>img').setAttribute('download','name-of-the-download-file-recommend-guid-or-timestamp.pdf');";
((JavascriptExecutor)driver).executeScript(script);
driver.findElement(By.xpath(".//*[#id='ctl00_SPWebPartManager1_g_29491a13_ee77_4e11_a5e4_e031cc13d245']/div[1]/table[1]/tbody/tr[2]/td[1]/a/img")).click();
Please note that the Css Selector in my example #ctl00_SPWebPartManager1_g_29491a13_ee77_4e11_a5e4_e031cc13d245>div>table>tbody>tr>td>a>img is just an example which was converted from your XPath. It might find an incorrect element.

unable to locate a element thru Selenium webdriver

I am trying to write a code to find cheapest tickets between two locations .I am stuck in the beginning of the code because selenium does not recognise simple input text box . here is the code .
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class orbitzsimpleTest {
public static void main(String args[]) throws InterruptedException {
WebDriver dr = new FirefoxDriver();
dr.navigate().to("https://www.orbitz.com/");
Thread.sleep(5000);
System.out.println("after threat sleep");
dr.findElement(By.xpath(".//*[#id='flight-origin']")).sendKeys("SFO");
}
}
I also tried to write the XPath using contains like
List<WebElement> s = dr.findElements(By.xpath(".//*[contains(#id,'flight_*')]"));
No luck. It looks so simple but i'm struggling to go past this.
You can just get this element with dr.findElement(By.id('flight-origin')) or (By.cssSelector('#flight-origin')).
And it's better to use 'smart' waits (wait for element to be displayed/clickable/visible) instead of sleep. Because it can be that 5 secs is not enough and you try to send key to the input which isn't loaded yet.
I think i found the reason why it was not recognising that element. It's because the flight tab had to be selected before it finds this text box .I was able to fix it .
Thank you so much for your input .

Intelligent Agents using Jade framework

I want to develop a multi-agent system and I want to run different agents from different containers. I'm using eclipse with Jade framework for this and i have no idea how to configure the "run configuration" for the project in order to accomplish this. So far I have this: -gui -container main:Sender;a1:Receiver;a2:Pong and I want to put agents a1 and a2 in a separate container. Please help.
When starting a new jade project I usually create a Coordinator agent with the methods to launch and destroy other agents. I think this is good practice as you can extend these methods to other agents if need be.
I hope that this will help.
First Run the Agent jade graphical user interface (Ejade) (By Installing the Ejade Library)
or you can run it on the console:
C:\java jade.Boot -gui (You have to fix the system variable path to "C:\..\jade.far" and create a variable name classpath = "C:\..\jdk7\")
Run the code that allows you to create a new container to deploy on it your agents.
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.domain.ams;
import jade.wrapper.AgentContainer;
import jade.wrapper.AgentController;
public class ContainerDeploy {
public static void main(String[] args) {
try{
Runtime runtime=Runtime.instance();
ProfileImpl profileImpl = new ProfileImpl(false);
profileImpl.setParameter(ProfileImpl.MAIN_HOST, "localhost");
AgentContainer agentContainer=runtime.createAgentContainer(profileImpl);
AgentController agentcontroller1 = agentContainer.createNewAgent("Name of Agent", "com.package.AgentClass", new Object[]{});
agentController1.start();
}catch(Exception e) {
System.out.println("Runtime Error\t");
e.printStackTrace();
}
}
}

JavaFX - Playing loop video

How should I loop a video in JavaFX?
I'm trying to just play a video one time after another, so I was looking for some sample code in many places and I could'nt make it work!
This is what doesn't work for me:
public MyMediaPlayer (){
media = new Media(getVideo());
mediaPlayer = new MediaPlayer(media);
mediaView = new MediaView(mediaPlayer);
startMediaPlayer();
}
private String getVideo() {
return getClass().getResource("videos/limbo.mp4").toString();
}
public final void startMediaPlayer() {
mediaPlayer.setMute(true);
mediaPlayer.setCycleCount(javafx.scene.media.MediaPlayer.INDEFINITE); //this is the line that should do the magic, but it doesn't...
mediaPlayer.play();
}
The following works for me (video loops forever). I can't replicate your issue.
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.media.*;
import javafx.stage.Stage;
public class VideoPlayerExample extends Application {
public static void main(String[] args) throws Exception { launch(args); }
#Override public void start(final Stage stage) throws Exception {
final MediaPlayer oracleVid = new MediaPlayer(
new Media("http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv")
);
stage.setScene(new Scene(new Group(new MediaView(oracleVid)), 540, 208));
stage.show();
oracleVid.setMute(true);
oracleVid.setRate(20);
oracleVid.setCycleCount(MediaPlayer.INDEFINITE);
oracleVid.play();
}
}
I'm under Java 7, doesn't work there . . . the problem seems to be MP4 format.
If you can't play MP4 files, either:
The MP4 is not encoded in a format JavaFX understands (the JavaFX 2.2 Media javadoc details the allowed formats).
OR
You don't have appropriate codecs installed on your machine to allow the MP4 file to be decoded. See the JavaFX 2.2 Media system requirements for information on what you need to install on your machine to allow MP4 files to be displayed.

How do I get Selenium 2 webdriver to work with Nightly (Firefox 64 bit)

I'm looking to execute a Selenium 2 test against the Nightly browser (FireFox 64bit). It records just fine with the Selenium IDE (v1.8.1). And it also plays back just fine using using the IDE. I then export the code to TestNG format. By the way I've loaded up the Webdriver Backed plugin so it exports WebDriver code for the Selenium 2 version. The problem I'm having is that when I export the code to TestNG format (Java) and execute it, the asserts never find the text on the screen. It executes fine so its not that the code didn't convert. It just seems to be something with the asserts. If I play it from the IDE plugin it finds it the text and asserts just fine, however as soon as it executes in Java it fails all the assertions. Any ideas to what might be going on. My code is below. Thanks much!
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static junit.framework.Assert.*;
import com.thoughtworks.selenium.Selenium;
public class TestWithConfig {
WebDriver driver;
Selenium selenium;
#BeforeMethod
public void startSelenium() {
driver = new FirefoxDriver();
selenium = new WebDriverBackedSelenium(driver,
"http://en.wikipedia.org/wiki/Main_Page");
}
#AfterMethod
public void stopSelenium() {
driver.close();
}
#Test
public void testTest() {
selenium.setSpeed("600");
selenium.open("/wiki/Main_Page");
assertTrue("face not found",selenium.isTextPresent("face"));
selenium.click("link=Contents");
selenium.waitForPageToLoad("30000");
assertTrue("Below not found",selenium.isTextPresent("Below"));
selenium.click("link=Toolbox");
selenium.click("link=What links here");
selenium.waitForPageToLoad("30000");
assertTrue("Pages not found",selenium.isTextPresent("Pages that link to"));
selenium.click("link=exact:Talk:Wine");
selenium.waitForPageToLoad("30000");
assertTrue("Some not found",selenium.isTextPresent("Some"));
}
}
Since your using selenium 2 and webdriver, Assert's work a little different. I can see that you using the WebDriverBackedSelenium. However keep in mind. That's not selenium2. That is just a way to ease into selenium 2. I would use something like this.
WebElement tooltip = driver.findElement(By.xpath("the xpath of the element"));
assertNotNull("Name:","IP Address:",tooltip);
What I'm doing here is. i'm looking for a tooltip.inside that tooltip, there are two main labels that stay the same: Name and IP Address:. So I'm testing to see if those words exists or not in the tool tip. The output should be Name: IP Address:. That tells me the answer is true.

Resources