Powershell to automate web site testing with modal dialog - javascript

Working on a quick Powershell script to use the IE object to enter values and submit a web page with a modal dialog. I want to be able to invoke the modal dialog, check some boxes on the dialog, and then close it and return to the page. I can invoke the modal dialog but nothing executes after the line to invoke the dialog until it is closed.
Powershell
$ie = new-object -com "InternetExplorer.Application"
$ie.visible = $true
$ie.navigate($url); do {sleep 1} until (-not ($ie.Busy))
$doc = $ie.Document
$books = $doc.getElementById(“searchBooks”).click();
Javascript
var ret = window.showModalDialog('selectBooks.aspx, books, ...);
I need a way to invoke commands in the context of the dialog. Tried running in the background and creating a runspace but no joy with either solution
& {$books = $doc.getElementById(“searchBooks”).click();}
(blocking)
$rs = [Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace()
$rs.Open()
$rs.SessionStateProxy.SetVariable("doc", $doc)
$p = $rs.CreatePipeline( { $books = $doc.getElementById(“searchBooks”).click(); } )
$p.Input.Close()
$p.InvokeAsync()
(not blocking, but I didn't have a handle to dialog elements)

That's an expected behavior of the modal dialog. If you want to interact with a dialog, perhaps showModelessDialog() is what you want.

Related

Open new window without URL. PHP/JavaScript

The problem currently:
I have a website that we are moving to a mobile application so I have put the website inside an IFrame (this has to be done not my choice). However when you click a certain link on the website (ON MOBILE) it will take you to a third party website or download page still inside the original Iframe. This is where the problem arises when you are on that third party website or download page there is no back button for mobile users (ios specifically).
My Ideas:
One way to fix this problem would be if the button is clicked the contents open up in a new window outside of the original Iframe on mobile.
Once the buy now button is clicked you will get directed to the third party website link.
This buttons code is not written in simple html or css though so there is no basic url set to the button. This means the window.open function will not work since a url is required and putting "_blank" attribute inside the php variable did not work either. Since my button is in a PHP variable called button that triggers a dynamic url through a CMS I am quite lost and not sure if this can be done.
What I want to happen is a new window to open with the third party website outside of the IFrame.
My PHP button
$button .= "<button class=\"btn btn-product-box get-coupon-btn deal-download ".$extraClass."\" onclick=\"myFunction()" . "\" data-action=\"".$purchaseAction."\">".$text."</button>";
I believe that the data-action attribute which holds the value $purchaseAction is related to the urls or links
I have added an onclick function to the button called myFunction which is a java script function that I am trying to write to trigger the button to open its contents in a new window.
My onclick function
function myFunction() {
document.getElementsByClassName('get-coupon-btn');
console.log("clicked myFunction");
var jsvar = '<?=$purchaseMethod?>';
console.log(jsvar);
}
I believe that $purchaseAction holds the URLs to the third party websites. But since they are grabbed from a CMS through twig programming language I do not have the http links for the sites and the button will trigger a different website depending on what button is clicked.
Here is more of the code
$purchaseMethods = $purchasable->purchaseMethods->orderBy(new Expression("Field(`typeId`, 32,18,19,31,17)"))->all();
// Log::error_log($purchasable->purchaseMethods->orderBy(new Expression("Field(`typeId`, 32,18,19,31,17)"))->getRawSql());
// $totalBtn = 0;
if(empty($purchaseMethods)){
$text = "REPORT ERROR";
if($short){
$text = "ERROR";
}
$button = "".$text."";
}
foreach($purchaseMethods as $loopIndex => $purchaseMethod){
switch ($purchaseMethod->type){
case "printMaterial":
$purchaseAction = $purchaseMethod->image->count() != 0? $purchaseMethod->image->one()->url : null;
$text = "GET COUPON";
if($short){
$text = "GET";
}
//$button .= "<button class=\"btn btn-product-box get-coupon-btn deal-download ".$extraClass."\" data-action=\"".$purchaseAction."\">".$text."</button>";
$button .= "<button class=\"btn btn-product-box get-coupon-btn deal-download ".$extraClass."\" onclick=\"myFunction()"."\" target=\"_blank" ."\" data-action=\"".$purchaseAction."\">".$text."</button>";
?>
<script>
//const { shell } = require('electron');
//var aHref = document.querySelector('#aHref');
function myFunction() {
document.getElementsByClassName('get-coupon-btn');
console.log("clicked myFunction");
//var jsvar = '<?=$purchaseMethod?>';
//console.log(jsvar);
}
</script>
Is there any way I can get the button to open its contents in a new window without using window.open or _blank since they both require urls?
You are calling myFunction(), when the button is clicked in Iframe.
Inside myFunction, you can open the URL using the following code which will open URL in a new tab outside Iframe
window.open(href, '_blank');
Here href is URL which you are grabbing from HTML

window.open and perform actions in console

In my js code, on a button click, I want to open the site in new tab and scroll to the selector's location, and highlight it.
Individually, I am able to achieve it, but when I try to peace it together, the selector is not highlighting
Following is the snippet which is to run in websites console
Website :- https://careers.mestel.com/
#go to website
window.open('https://careers.mestel.com/');
#Scroll to the view where selector is present
document.querySelector('ppc-content[key="footer-mestelFooterContent11"]').scrollIntoView();
# Highlight the selector
css_highlight = document.querySelector('ppc-content[key="footer-mestelFooterContent11"]')
css_highlight.style.outline = '#FDFF47 solid 5px'
Only window.open action is getting performed, other actions are not getting perform. Please suggest how to inject remaining js code after window.open. The final expected result will be is as shown in the following image
It is because the code will continue to execute in the window that is currently running (not the new opened one). You can inject new code in a new window in this way (not the only way):
var newWindow = window.open('https://careers.mestel.com/');
var newScript = document.createElement('script');
function injectScript() {
// The code to inject
}
newScript.innerHTML = injectScript.toString() + ';';
newWindow.document.body.appendChild(theScript);

UIWebview Print option

I've created a container iOS app(objective c) which has only one webview. The webview is loaded with a website which is not owned by me. The website that i am accessing from webview has a print button. Tapping on it doesnt do anything. The same website when accessed via mobile browser works fine. I tried with several other websites with print button . It acts the same.
I wrote the following code for airprint. So whenever a native print button is clicked the following code is executed and it works fine.
But how can i execute this code when user clicks on print button on website ???
In other ways is there a way to detect if user clicks the print button on any website ??
UIPrintInfo *pi = [UIPrintInfo printInfo];
pi.outputType = UIPrintInfoOutputGeneral;
pi.jobName = webView.request.URL.absoluteString;
pi.orientation = UIPrintInfoOrientationPortrait;
pi.duplex = UIPrintInfoDuplexLongEdge;
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.printInfo = pi;
pic.showsPageRange = YES;
pic.printFormatter = webView.viewPrintFormatter;
[pic presentAnimated:YES completionHandler:^(UIPrintInteractionController *pic2, BOOL completed, NSError *error) {
// indicate done or error
}];

Close Flipkart open pop-up and go to main window using Selenium

WebDriver driver = new FirefoxDriver();
driver.get("https://www.flipkart.com");
driver.manage().window().maximize();
String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler);
I tried it by switching to main window also. Please add valuable input or code to close the pop up.
You can try using the java Robot API by importing java.awt.Robot libraries. An example is here:
One solution for File Upload using Java Robot API with Selenium WebDriver by Java
You can try to use it similarly to press the Esc key. Pressing Esc on flipkart website gets rid of the pop-up.
The pop-up which appears on Flipkart's website is a simple HTML modal. Window handle is used when a new pop-up window needs to be accessed.
To close the pop-up just click on the cross on the top right corner of the pop-up. Use waits to ensure that selenium finds the WebElement.
Try this:
driver.get("https://www.flipkart.com");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement cross = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.className("close-icon")));
cross.click()

How does or is it possible for a browser keep track of a pop up window

I noticed when using the Google Chat feature from your Google Mail that you can pop out a chat into its' own window. Then when I went to close the Google Mail window it warned that other windows would also be closed if I continued and then showed a list of the windows. How is this done? Is it possible to track whether a window you invoked has been closed?
Yes, when you do a window.open in JavaScript, you can set it to a var like:
myWindow = window.open(...)
Then you could bind events to that window like:
myWindow.onUnload = funcWindowClosed;
You could keep track of multiple pop-up windows by putting them into an array.
You can also call JavaScript functions in the parent window by the child (pop-up) window using:
window.opener
If you assign each new window a name when u make them a popup and then with php (or anything else) save the name of the window to a cookie or a session, you can then close all of the popups created by your site when a certain action is taken:
this is what i do (in php/js):
function closePopups(){
var popups = Array();
<?
$i = 0;
if(isset($_SESSION['popups'])){
foreach($_SESSION['popups'] as $key=>$pop){
echo "popups[$i] = '$pop';\n";
$i++;
}
unset($_SESSION['popups']);
}
?>
for( i = 0; i < popups.length; i++ ){
window.open('',popups[i],'width=1,height=1').close();
}
}
In the for loop you have to open the popup first and then close it so that way your current window can see if a popup with that name was already opened, and if it was, than it will close it. If it wasn't it will create a new window with the same name and immediately close it.
This is how i soleved the issue

Categories