I am new to webdrivers and I am having the system choose whether it is a create or save button for two different scenarios and differentiate by the ids.
So far I have
var createButton = webDriverConfig.getWebDriver().findElement(By.id(createButtonId));
var jsExecutor = ((JavascriptExecutor) webDriverConfig.getWebDriver());
if(createButton.getText().equals("//button[#title='Create']")) {
jsExecutor.executeScript("arguments[0].scrollIntoView();", createButton);
createButton.click();
} else {
var saveButton = webDriverConfig.getWebDriver().findElement(By.id(saveButtonId));
jsExecutor.executeScript("arguments[0].scrollIntoView();", saveButton);
saveButton.click();
}
However when I ran the system it fails and I am not sure how it can differentiate whether it is a create scenario vs update scenario to save when it clicks the button when it finishes filling out the form.
Related
I am at my wits end, trying to automate our tests for the Windows SaveAs-dialog.
The thing is that the automation code works on some machines but not all.
It works on my local box and a few other, but we need to make it work on all our test machines. Something is different but what I can see sofar is
Same windows version
Same dotnet --info
The test code I have tried to make work is something like
...
var app = FlaUI.Core.Application.Launch("FlaUISaveDialog.exe");
using (var automation = new UIA3Automation())
{
var window = app.GetMainWindow(automation);
var button1 = window.FindFirstDescendant(cf => cf.ByAutomationId("ClickIt"))?.AsButton();
button1?.Patterns.Invoke.PatternOrDefault.Invoke();
Thread.Sleep(2400); // Wait for window to appear!
var dialog = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Window));
Thread.Sleep(1000);
var fileNameTextBox = dialog.FindFirstDescendant(e => e.ByAutomationId("1001"));
fileNameTextBox.Focus();
fileNameTextBox.Patterns.Value.Pattern.SetValue(resultFile);
Thread.Sleep(2400);
//FlaUI.Core.Input.Keyboard.Press(VirtualKeyShort.RETURN);
var save = dialog.FindFirstChild(e => e.ByAutomationId("1").And(e.ByControlType(ControlType.Button)));
save.Focus();
var mousePoint = new Point(save.BoundingRectangle.X + save.BoundingRectangle.Width/2, save.BoundingRectangle.Y + save.BoundingRectangle.Height/2);
FlaUI.Core.Input.Keyboard.Press(VirtualKeyShort.RETURN);
//FlaUI.Core.Input.Mouse.Click(mousePoint);
//save.Patterns.Invoke.Pattern.Invoke();
Thread.Sleep(2400); // Wait for file save to complete
Assert.IsTrue(File.Exists(resultFile));
}
After another day going at this, I found that it seems to be the "SetValue" pattern that looks like it works, but doesn't change the Dialogs actual filename.
But moving the mouse,clicking and typing like below actually works:
var fileNameTextBox = dialog.FindFirstDescendant(e => e.ByAutomationId("1001"));
var mousePoint = new Point(fileNameTextBox.BoundingRectangle.X + fileNameTextBox.BoundingRectangle.Width/2, fileNameTextBox.BoundingRectangle.Y + fileNameTextBox.BoundingRectangle.Height/2);
Thread.Sleep(1000);
FlaUI.Core.Input.Mouse.MoveTo(mousePoint);
FlaUI.Core.Input.Mouse.Click(mousePoint);
Thread.Sleep(1000);
FlaUI.Core.Input.Keyboard.TypeSimultaneously(VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_A);
Thread.Sleep(1000);
FlaUI.Core.Input.Keyboard.Type(resultFile);
Thread.Sleep(1000);
FlaUI.Core.Input.Keyboard.Press(VirtualKeyShort.RETURN);
Edit #2: I seems that it is the Windows Explorer option "View File name extensions" that affects the automation behavior of the "SaveFileDialog". If we turn on "View file name extensions" the "SetValue" pattern starts working. Turn the setting off and the "SetValue" stops working! Unexpected, to say the least!
I have an Ecommerce website and on one page there is a button named Place Order. When I click on the Place Order button, then it allows me to open a new window named Paypal. I need to stay on the same tab without opening the new window. After that I need to click on the element for that Paypal page.
How can I do this?
My code is as follows:
String parent_handle= driver.getWindowHandle();
System.out.println(parent_handle);
driver.findElement(By.xpath(".//*[#id='co-place-order-area']/div[2]/div[3]/div/button")).click();
new WebDriverWait(driver,10).until(ExpectedConditions.numberOfWindowsToBe(1));
Set<String> handles = driver.getWindowHandles();
System.out.println(handles);
for(String handle1:handles)
if(!parent_handle.equals(handle1))
{
driver.switchTo().window(handle1);
System.out.println(handle1);
}
I don't know about java but in C# you would use PopupWindowFinder class
var target = driver.findElement(By.xpath(".//*[#id='co-place-order-area']/div[2]/div[3]/div/button"));
PopupWindowFinder finder = new PopupWindowFinder(driver);
var parent = driver.CurrentWindowHandle;
string newHandle = finder.Click(target);
driver.SwitchTo().Window(newHandle);
Then after you deal with the new window you can switch back to the parent window.
I have a scenario like when I click on a link it opened in new tab. Using Selenium WebDriver how can we handle it.
As per my knowledge we can't switch to new tab but when I search in Web, got some below solutions.
ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));
driver.close();
driver.switchTo().window(tabs2.get(0));
Unfortunately, every given solution contains driver.getWindowhandles(). But AFAIK even when a browser has multiple tabs it always returns only one handle.
My scenario is, when I click on one button it opens in new tab.Could any one please provide some solution to
Switch between Tabs or
How to open that tab in new window.
When your new tab has opened,then after that you are in any certain tab of the window.Now, you can use keys.chord(keys.ctrl,keys.tab) for switching between tabs. By using keys, we can take the keyboard i/p.
Write a method to switch the handle of a driver to a new window/tab based on the windows title:
public void SwitchHandleToNewWindow(IWebdriver driver, string windowTitle)
{
ReadOnlyCollection<string> handles = driver.WindowHandles;
foreach(string handle in handles)
{
driver.SwitchTo().Window(handle);
if(driver.Title.Contains(windowTitle))
{
return;
}
}
}
The code is straight forward, so implementation is straightforward too. If you want to switch to a new tab then you do something like : SwitchHandleToNewWindow(driver,"Test Page")
Iam trying to switch between browser ie on click of a button it launches a new browser it
is finding the handle ..the problem is it is not able to find the object inside the new browser searched with id,xpath,name etc can some one give me any suggestion on the same.
also it is able to match the url as well.
please provide me the solution on the same.
below is the code.
//Previous screen
Set windows = driver1.getWindowHandles();
driver1.findElement(By.id("findButton")).click();
//switching handle for the new screen
driver1.switchTo().window("Customer Search");
driver1.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
int i = 1;
while(i<= 10){
for (String handle : driver1.getWindowHandles()) {
String myTitle = driver1.switchTo().window(handle).getTitle();
System.out.println("myTitle value : " +myTitle);
//customer search is the new window title
if(myTitle.equalsIgnoreCase("Customer Search")){
driver1.manage().window().maximize();
//if i pass the right url of the screen that is also matching here i have given dummy("sshsj")
if(driver1.getCurrentUrl().equalsIgnoreCase("sshsj"));
{
System.out.println("Url is matching");
//But not able the recognise the object on the new window.
driver1.findElement(By.xpath("html/body/left/form/table/tbody/tr[2]/td[1]/input")).sendKeys("kamal");
}
You can use JS to open a new window, it's faster.
IJavaScriptExecutor jscript = driver as IJavaScriptExecutor;
jscript.ExecuteScript("window.open()");
Then to switch windows, use the window handles:
List<string> handles = driver.WindowHandles.ToList<string>();
driver.SwitchTo().Window(handles.Last());
driver.get(url);
driver.findElement(By.xpath("html/body/left/form/table/tbody/tr[2]/td[1]/input")).sendKeys("kamal");
It is possible that the element may be present inside an iframe. In that case, you need to switch to that iframe before you can access any element inside the iframe.
I use Entity Framework 4 and Self Tracking Entities. The schema is like:
Patient -> Examinations -> LeftPictures
-> RightPictures
So there is TrackableCollection of these two relationships Patient 1 - * ....Pictures.
Now when loading the customers Form and browsing the details I dont need to load these
data images, only when another form is loaded for Examination details!
I am using a class library as a Data Repository to get data from the database (SQL Server) and this code:
public List<Patient> GetAllPatients()
{
try
{
using (OptoEntities db = new OptoEntities())
{
List<Patient> list = db.Patients
.Include("Addresses")
.Include("PhoneNumbers")
.Include("Examinations").ToList();
list.ForEach(p =>
{
p.ChangeTracker.ChangeTrackingEnabled = true;
if (!p.Addresses.IsNull() &&
p.Addresses.Count > 0)
p.Addresses.ForEach(a => a.ChangeTracker.ChangeTrackingEnabled = true);
if (!p.PhoneNumbers.IsNull() &&
p.PhoneNumbers.Count > 0)
p.PhoneNumbers.ForEach(a => a.ChangeTracker.ChangeTrackingEnabled = true);
if (!p.Examinations.IsNull() &&
p.Examinations.Count > 0)
p.Examinations.ForEach(e =>
{
e.ChangeTracker.ChangeTrackingEnabled = true;
});
});
return list;
}
}
catch (Exception ex)
{
return new List<Patient>();
}
}
Now I need when calling the Examination details form to go and get all the Images for the Examination relationship (LeftEyePictures, RightEyePictures). I guess that is called Lazy Loading and I dont understood how to make it happen while I'm closing the Entities connection immidiately and I would like to stay like this.
I use BindingSource components through the application.
What is the best method to get the desired results?
Thank you.
Self tracking entities don't support lazy loading. Moreover lazy loading works only when entities are attached to live context. You don't need to close / dispose context immediately. In case of WinForms application context usually lives for longer time (you can follow one context per form or one context per presenter approach).
WinForms application is scenario for normal attached entities where all these features like lazy loading or change tracking work out of the box. STEs are supposed to be used in distributed systems where you need to serialize entity and pass it to another application (via web service call).