I am trying to make a script for page, all I need is just simple java script, if there is a text on the page in example - no found the macros should hit the button find work. The macros must hit the button find work in 1 second interval.
Sorry for my english
This is an example how you could make JavaScript script for iMacros. The main part is CONTENT=EVENT:MOUSEOVER since that part hovers over a page element. If element is present it will return true else it will return false. Try it out.
var macroTest;
macroTest ="CODE:";
macroTest +="TAG POS=1 TYPE=INPUT:CHECKBOX FORM=NAME:TestForm ATTR=NAME:C9&&VALUE:ON CONTENT=EVENT:MOUSEOVER"+"\n";
if(iimPlay(macroTest)>0)
{
alert("Checkbox found");
}
else
{
alert("Checkbox not found");
}
Related
I want to set up only one instance of the CHM file when clicking on "Help" in the menubar and stopping it from opening twice when clicked again - therefore how do I code it?
I've tried to use it with process.isAlive(), but after I close it I want a counter set to zero, which only opens another CHM file when the counter is 0.
helpMenu.addMouseListener(new MouseAdapter() {
// do this after clicked
openCHM();
});
So MouseEvent is fired once.
openCHM() {
Process p;
if(cnt == 0) {
p = Runtime.getRuntime().exec("hh.exe Help.chm");
cnt++;
if(!p.isAlive()) {
cnt = 0;
}
}
I expected the counter to be 0, but then came to the conclusion that MouseEvent already fired once and the code got already executed, therefore it never goes to the second if-statement and sets my counter to 0.
EDIT
There is no correct answer how to open a CHM file once, but there is a workaround that makes it possible, we just need to look if the file is renamable or not.
protected void openCHM() {
try {
File file = new File("YOURNAME.chm");
boolean renamable = file.renameTo(file); // can't rename if file is already open, returns true if renaming is possible
if(renamable) {
Runtime.getRuntime().exec("hh.exe YOURNAME.chm");
} else if(!file.exists() ){
// message: file doesn't exist (in path)
} else {
// file is already open
}
} catch () {
}
}
I'm not a Java programmer but the short story - not possible (AFAIK).
You know, hh.exe is the HTMLHelp executable and associated with *.CHM files. It's just a shell that uses the HTML Help API and is really just hosting a browser window.
HH.EXE is not single instance, if you open a CHM or another file three times using HH.EXE, then three help windows will appear. Try it using PowerShell:
PS D:\_working> hh.exe C:\Windows\Help\htmlhelp.chm
Several client-side command line switches are available to help authors that are part of the HTML Help executable program (hh.exe) and therefore work when HTML Help Workshop is not set up.
KeyHH.exe was running years ago with special parameters.
If you call the HH API directly from your application, and not via a second helper program like HH.EXE or KEYHH.EXE, then you MUST close any open help windows before shutting down the application or you will probably crash Windows.
For some information related to the problem you maybe interested in Open CHM File Once.
Some quoted info from the link above:
When you do that you are just calling the help viewer again and again from the command line, you're not using the HTML Help API which is what you need to access the CHM once it is open. You need to check whether your flavors of Java and Smalltalk support calls to the HTML Help API. This API is documented in detail in the help file of Microsoft HTML Help Workshop, which is the compiler package you installed to be able to generate CHMs.
I have this funny bug happening to me today. I've been using Selenium for years already and never had an issue navigating to URL (via driver.navigate().to(url)) however today I'm attempting to navigate to a specific URL and I find that after executing the program several times it sometimes just stays on the original page without navigating to new page.
The funny thing is that this only happens about 50% of the time and it only happens when navigating to a specific URL at a specific part of the program (in other parts of program I have no issue navigating to this url).
Is it possible that some element on the current page is preventing driver.navigate().to(url) from executing?
I've looked at this and this question but both seem to have issues with navigating altogether. In my case, it sometimes works and sometimes doesn't (even when the exact same url is being used).
Also I'm not getting any specific errors (so I don't have much more info to post). The program just moves on as if the statement didn't exist.
I'll be happy to provide additional details if necessary.
Code:
shoppingCartURL.navToShoppingCart(driver);
String[] XPath = { "//*[contains (#value,'Delete')]" };
List<WebElement> elementList = webElementX.getElementListByXPath(driver, XPath);
System.out.println("Deleting " + elementList.size() + " element(s) from shopping cart");
for (int elementListCounter = 0; elementListCounter < elementList.size(); elementListCounter++) {
WebElement singleElement = elementList.get(elementListCounter);
try {
singleElement.click();
} catch (Exception e) {
System.out.println("Exception occurred (StaleElementReferenceException)");
}
}
if (conditionX == false) {
productPage.navToProductPage(driver, product); // this method is not always executed, program continues to execute productPage.performActionOnPage(driver); without navigating to 'product page'
productPage.performActionOnPage(driver);
}
public void navToProductPage(WebDriver driver, String product)
{
String URL = "https://www.example.com/product/" + product;
System.out.println("Navigating to " + URL); // always prints the correct url (but still doesn't always navigate to url as mentioned in question)
driver.navigate().to(URL);
}
Update:
I noticed ref=cart-ajax-error in redirect url (after deleting items from cart). Apparently, the site is using AJAX to refresh page after deleting items from cart. Might this conflict with my attempt to navigate to another page? In other words, perhaps Selenium is getting two different messages at the same time.. refresh page and navigate to new page.. so it remains on the current page?
If this is true, what can be done to resolve this issue?
Thanks!
When it sometimes happens and sometimes not it's nearly always a matter of timing.
Add an explicit wait to your code after navigating to the URL:
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.urlToBe(URL));
I already have this kind of error, and I still don't know why. What am I doing wrong?
I need to assert true if I get a text in the page source.
So here is my method:
public boolean AssertSearch() {
return driver.getPageSource().contains("Item found");
}
And here is my assert:
assertTrue(buscarnok.validabuscaNOK());
And I keep receiving the message "Assertion Error". I don't know why. If I change the "return driver.getPageSource().contains("Item found");"to driver.findelement(by.id("someID")).isdisplayed();it works fine, so why isn't it working with getpagesource?
If the text you are looking for is not initially in the page or if it is hidden, it may not find it.
Try something like this:
String bodyText = driver.findElement(By.tagName("body")).getText();
Assert.assertTrue("Item Found", bodyText.contains(text));
You can narrow down the search by selecting a different tag name or even a div by its class or id
On my web page I am running an applet and in order to warn user for certain inputs to the applet, I am using printing the errors to the html page. In order to do this I am using javascript applet communication. Below is the code for the Javascript:
<script type="text/javascript">
var counter=0;
function RemoveAppletErrorMessage()
{
var AppletErrorTag=document.getElementById("AppletErrorMessages");
if(AppletErrorTag!=null)
{
document.body.removeChild(AppletErrorTag);
}
}
function updateWebPage(s)
{
if(counter>=1) RemoveAppletErrorMessage();
var parag=document.createElement('P');
parag.setAttribute("id","AppletErrorMessages");
var txt=document.createTextNode(s);
parag.appendChild(txt);
document.body.appendChild(parag);
counter++;
}
</script>
Here, the Javascript function updateWebPage(s) function is called from the Java applet when a JButton is clicked with the following code:
if(jso != null && !errorMessage.isEmpty())
try {
jso.call("updateWebPage", new String[] {errorMessage});
return;
}
catch (Exception ex) { ex.printStackTrace(); }
In terms of communication everything works fine. However, when the JButton is clicked the second time, I am trying to clear up the error messages with RemoveAppletErrorMessage() calling from updateWebPage(s). It seems like every time I hit the JButton the error messages are appended to the web page which I do not want. I have tried to clear it with .innerHTML='' which did not work in the first place, so I changed my strategy to adding a node and checking if the node exists, clearing that node and adding again. What might be going on wrong?
May be you're appending to your errorMessage variable in Java?
I'd open a div with an id of AppletErrorMessages and use jQuery in my updateWebPage() function like this (e.g. stop creating a new paragraph every time).
function updateWebPage(s)
{
$("#AppletErrorMessages").html(s);
}
I'm updating a Selenium program I wrote a while back and part of it has stopped working. I want to go through a whole series of links on a page, click on each, making sure that some expected text is present. But sometimes a log-in page (https://library.med.nyu.edu/sso/ezproxy_form.php) appears before the desired page, in which case I need to log in before checking the page. The problem is, no matter what string I put in to check whether I've landed on the log in page, Selenium concludes it's not present and skips logging in, obviously causing everything else to fail. See below--I'm not sure that was actually the problem. It seems to be instead that it's rushing through the "if we need to sign in" code without actually signing in, then obviously failing the main part of the test because it's not on the right page.
Here's the code--does anyone see my mistake?
for (int i = 0; i < Resources.size(); i++) {
try {
selenium.open("/");
selenium.click("link=" + Resources.get(i).link);
selenium.waitForPageToLoad("100000");
if (selenium.isTextPresent("Please sign in to access NYUHSL e-resources")) {
selenium.type("sso_user", kid);
selenium.type("sso_pass", password);
selenium.click("name=SignIn");
selenium.waitForPageToLoad("100000");
}
if (!selenium.isTextPresent(Resources.get(i).text)) {
outfile.println(Resources.get(i).name + " failed");
}
} catch (Exception e) {
outfile.println(Resources.get(i).name + " could not be found--link removed?");
}
}
Does the login page have a page title? If yes, try validating the page title using selenium.getTitle() method to check if you are headed to login page. If not, proceed clicking on the link without logging in.
I think page title validation can help resolve this issue
Try putting:
selenium.setSpeed("1000");
Right after the selenium.open this will inject 1 second delay (1000ms) between selenium commands. You should make it a standard practice to add this, especially if you're not running headless browsers.
Also you might consider using, since you know the url you are expecting to be on if on the login page, the selenium command getLocation. This will return the absolute URL of the current page. Might be more effective than trying to look for elements that can change at any time within the page.
So to use getLocation in your code above:
if (selenium.getLocation() == "your reference url"){
do your login stuff here
}
Again this is just a sample to illustrate what I'm saying. Hope it helps you out.