How to right click on Selenium with all UI grid rows selected? - c#

I need to keep both UI Grid rows selected in order for a right click customised menu to show up. If only one row is selected a different context menu is displayed so I need to keep both rows selected.
Any ideas how to do this?
Code below is what I have tried. Trouble is with this code, even though both rows are selected, the right click only happens for one row (effectively de-selecting one of the rows)
See code below
class Program
{
static void Main(string[] args)
{
IWebElement tableElement;
String _address = "https://datatables.net/examples/api/select_row.html";
IWebDriver _driver = new ChromeDriver();
_driver.Navigate().GoToUrl(_address);
tableElement = _driver.FindElement(By.Id("example"));
Actions actions = new Actions(_driver);
var noRows = _driver.FindElements(By.XPath("//table[#id='example']/tbody/tr"));
for (int i = 0; i < 2; i++)
{
noRows[0].Click();
actions.KeyDown(Keys.Control).Click(noRows[1]).KeyUp(Keys.Control).Perform();
actions.ContextClick(noRows).Perform();
}
}
}

Related

Selenium C#: How to Find Element Again

First time here. So I was wanting some help on a selenium testing that's got me stuck now for several hours.
So I stored a IList<IWebElement> on a page, composed of <a>. I was able to click the 1st element of that IList through a foreach, get what I need from the new page and go back to the page of the list using driver.manage.navigate.back().
However, this time, I can't click the 2nd element of the list.
Is there a way to find that 2nd element, 3rd element, 4th element, etc?
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
string url = "http://DummyPageOne.Com";
driver.Navigate().GoToUrl(url);
driver.Manage().Timeouts().ImplicitlyWait(Timespan.FromSeconds(10));
PageOne page = new PageOne();
foreach (IWebElement item in page.Items)
{
item.Click();
ItemDetails details = new ItemDetails();
details.SaveImage(#"D:\Images\");
driver.Navigate().Back();
}
}
public class PageOne()
{
public PageOne()
{
PageFactory.InitElements(driver, this);
}
public IList<IWebElement> Items;
public void StoreItems()
{
string locator = "DummyLocator";
Items = driver.FindElements(By.Xpath(locator));
}
}
public class ItemDetails()
{
public ItemDetails()
{
PageFactory.InitElements(driver, this);
}
public void SaveImage()
{
}
}
On the basis of what I understood from your question, here is the way to achieve that. I checked the following C# Code on Chrome and it's working perfectly. I hope this helps.
[Test]
public void ClickAllLinks()
{
//Navigate to URL
driver.Navigate().GoToUrl(#"https://www.google.co.in/#q=Selenium");
//Store Links in IList
IList<IWebElement> resultLinks = driver.FindElements(By.CssSelector("div#ires>ol>div:not(:first-child)>div>div>h3>a"));
//Print Count
//Console.WriteLine(googleLinks.Count);
for (int i = 0; i < resultLinks.Count; i++)
{
int j = i + 1;
//Click on Link
driver.FindElement(By.CssSelector("div#ires>ol>div:not(:first-child)>div:nth-child(" + j + ")>div>h3>a")).Click();
//Print Element Locator
//Console.WriteLine("div#ires>ol>div:not(:first-child)>div:nth-child(" + j + ")>div>h3>a");
Thread.Sleep(2000); //Static wait is not recommended, add conditional wait
//Get what you need from new page
//Navigate back parent page
driver.Navigate().Back();
Thread.Sleep(2000); //Static wait is not recommended, add conditional waits
}
}
If this is not what you want please let me know.
The simple way is to initialize that list again in the end of loop.
Ok, the thing to remember about .Net and Selenium, is that the C# variables/types are references to things that exist in memory (on screen) in the browser.
When the browser window navigates away, the reference held by those variables will be invalid. You must 're-find' them.
I would be very surprised if you could click the first item in the list either.
The only way to click on these objects, is to reacquire the object after the page has reloaded.
Eg: Driver.FindElement(By.XPath("\\div[#class='somelocator']"));
If you can, I would recommend constructing a collection of 'By' classes.
Then loop through the By classes using them in the Find method.
That way, each iteration of the loop, you will be acquiring the object fresh.

Cannot edit DataGridView cell, after choosing option I have to press Esc and lose the edit

So here is the story - I have 2 DataGridViews - one of them is used as a display, the other one is user for editing or adding new entries. I don't use bindings on the edit one. When I am creating new entry I am just extracting the cells' values and passing them to an object. When I am editing though comes the problem.
The edit consists of 2 parts - one is to select the entry to be edited and display it on the second DGV. This is done with SelectionChanged event and the code is bellow. Then when editing is done it should just publish the new task the same way as creation. The thing is that when I am doing the edit and I come to the moment when I have to select from one of my cells - a ComboBoxCell - and I make a selection everything freezes. I cannot click anything else than this ComboBox until I press Esc - which obviously reverts the choice.
Why is this freeze happening and what causes it only when the data is cloned from the first DGV, but is okay when creating a brand new row? I am using VS2012 by the way and this is a windows forms application
Here is the code for duplication of the selected task - this method is the only thing in the DGV1's SelectionChanged Event handler:
public void createTemplateTaskToBeEditted(Form1 form1, ApplicationControl appControl)
{
form1.dg_templateView.Rows.Clear();
for (int i = 0; i < form1.dg_taskView.SelectedRows.Count; i++)
{
if (form1.dg_taskView.SelectedRows[i].Cells[0].Value == null)
{
form1.dg_taskView.SelectedRows[i].Cells[0].Value = false;
}
int index = form1.dg_templateView.Rows.Add();
form1.dg_templateView.Rows[index].Cells[0].Value =
form1.dg_taskView.SelectedRows[i].Cells[0].Value.ToString();
form1.dg_templateView.Rows[index].Cells[1].Value =
form1.dg_taskView.SelectedRows[i].Cells[2].Value.ToString();
form1.dg_templateView.Rows[index].Cells[2].Value =
form1.dg_taskView.SelectedRows[i].Cells[3].Value.ToString();
form1.dg_templateView.Rows[index].Cells[3].ValueType = typeof(ComboBox);
form1.dg_templateView.Rows[index].Cells[3].Value =
form1.PopulateAssignToComboBox(appControl.GetAllowedMembers(form1));
form1.dg_templateView.Rows[index].Cells[4].Value =
form1.dg_taskView.SelectedRows[i].Cells[5].Value.ToString();
form1.dg_templateView.Rows[index].Cells[5].Value =
form1.dg_taskView.SelectedRows[i].Cells[6].Value.ToString();
form1.dg_templateView.Rows[index].Cells[6].Value =
form1.dg_taskView.SelectedRows[i].Cells[7].Value.ToString();
form1.dg_templateView.Rows[index].Cells[7].Value =
form1.dg_taskView.SelectedRows[i].Cells[8].Value.ToString();
form1.dg_templateView.Rows[index].Cells[8].Value =
form1.dg_taskView.SelectedRows[i].Cells[9].Value.ToString();
form1.dg_templateView.Rows[index].Cells[9].Value =
form1.dg_taskView.SelectedRows[i].Cells[10].Value.ToString();
form1.dg_templateView.Rows[index].Cells[10].Value =
form1.dg_taskView.SelectedRows[i].Cells[11].Value.ToString();
form1.dg_templateView.Rows[index].Cells[11].Value =
form1.dg_taskView.SelectedRows[i].Cells[12].Value.ToString();
}
}

enable only 1 button in a list?(unity3D)

I have a code which spawns a button(gameobject) with presets everytime I press save.
This button is then saved into a List of gameobjects which I then use to select or deselect them.
I tried to make it so that only 1 button in the list can be selected at a time.
It's working to some degree as If i press the button before the current selected one it works. But if i press any button after the currently selected one it is unable to deselect it.
Can anyone help me with this?
public List<GameObject> presetButtons = new List<GameObject>() ;
This is the list of Buttons
void Update () {
for (int i = 0; i < presetButtons.Count; i++) {
if(presetButtons[i].GetComponent<CamUIButtonHandler>().isSelected){
indicator = i;
topView.isSelected = false;
}
}
for (int u = 0; u < presetButtons.Count; u++) {
if(u != indicator)
presetButtons[u].GetComponent<CamUIButtonHandler>().isSelected = false;
}
}
This is supposed to deactivate the other buttons once 1 is selected.
I'm guessing that there is something that causes it to not be able to overwrite the currently selected button however I can't seem to find the issue.
Can anyone help me with this? much thanks
This sounds to me like a good place to use UGUI's Toggle And ToggleGroup features. Put a toggle component on each button. Then set each of those buttons' group properties to be the same ToggleGroup. Of course if you aren't using UGUI then this won't be possible.

Clicking on List Item on Dropdown

I'm having a problem in interacting with a custom dropdown control. It works fine the 1st 6 times, but after that, since the screen is resized, it could no longer locate and click the option in the dropdown control, returning an exception - can't click on hidden control. I tried putting in a itemField.DrawHighlight(); on the control I'm looking for, and it finds it, however it can't click on it. I also tried a to scroll down, but it seems to be not working.
bool addItemCheck = false;
int scrollCheck = 0;
while (Check == false)
{
var addItem= new HtmlButton(window);
addItem.SearchProperties.Add(HtmlButton.PropertyNames.Id, "add-new-item");
Mouse.Click(addItem);
scrollCheck = scrollCheck + 1;
if (scrollCheck > 6)
{
Mouse.MoveScrollWheel(window, -100);
}
var itemDropDown = new HtmlSpan(window);
itemDropDown .SearchProperties.Add(HtmlSpan.PropertyNames.Class, "item-dropdown");
itemDropDown .SearchProperties.Add(HtmlSpan.PropertyNames.InnerText, "Select an Item");
Mouse.Click(itemDropDown );
addItemCheck = itemDropDown.Exists;
}
bool itemBoxCheck = false;
HtmlCustom itemBox = null;
while (itemBoxCheck == false)
{
itemBox = new HtmlCustom(window);
itemBox.SearchProperties.Add(HtmlCustom.PropertyNames.Id, "item-listbox");
var itemField = new HtmlCustom(itemBox);
itemField .SearchProperties.Add(HtmlCustom.PropertyNames.InnerText, item);
Mouse.Click(itemField);
itemBoxCheck = itemBox.Exists;
}
I would really appreciate any help. Thank you.
Try calling the method InsureClickable() on the control before attempting to click on it.
for example:
itemDropDown.EnsureClickable();
Mouse.Click(itemDropDown);
Edit:
if this doesn't work you'll have to scroll down to the item.
try using:
Mouse.MoveScrollWheel()
if that doesn't work also you'll have to map the scroll control and click on it.

web user control adding items problem

On one web user control
public void displayFindingSection(int sectionsid,string text,string head)
{
SectionHeading.Text = head;
DataSet totImgs;
totImgs = objGetBaseCase.GetFindingsNewerImages(sectionsid);
FindingViewerlist.DataSource = totImgs;
DataBind();
SectionText.Text = text;
}
On other web user control
public void DisplayFindingsViewer(CipCaseWorkflowItem2 item)
{
FindingViewerDisplay.Visible = true;
ImageAndSimpleViewer.Visible = false;
objGetBaseCase.GetFindingsImages((Convert.ToInt32(Session["CaseId"])), item.ItemId);
FindingsViewerNew = objGetBaseCase.GetFindingViewerNewElementDetails(item.ItemId);
for (int i = 0; i < FindingsViewerNew.Count; i++)
{
FindingViwerDisplay uc = (FindingViwerDisplay)LoadControl("FindingViwerDisplay.ascx");
FindingPlaceholder.Controls.Add(uc);
uc.displayFindingSection(Convert.ToInt32(FindingsViewerNew[i].Index), FindingsViewerNew[i].Text, FindingsViewerNew[i].Title);
}
}
I am adding the all the image in user control and displaying the image, but when i am using the above code, web user control is also adding every time and one image is showing in in control what i want is all images should show in only one user control.. sectionsid is getting the image id from the database. I think prob with for loop but i am unable to solve it.. help me it that
Might be it is happening u have defined it inside the loop
FindingViwerDisplay uc = (FindingViwerDisplay)LoadControl("FindingViwerDisplay.ascx");
FindingPlaceholder.Controls.Add(uc);
On Each loop you are adding uc and calling displayFindingSection whcich ofcouse add 1 image than loop go back add a new control again and than add one image it will go on till your loop completion so add control once before loop and call just displayFindingSection in loop..
Do this,
FindingViwerDisplay uc = (FindingViwerDisplay)LoadControl("FindingViwerDisplay.ascx");
FindingPlaceholder.Controls.Add(uc);
//define here a dataTabel with three columns let say u have datatable dt
for (int i = 0; i < FindingsViewerNew.Count; i++)
{
dt.Rows.Add(Convert.ToInt32(FindingsViewerNew[i].Index), FindingsViewerNew[i].Text, FindingsViewerNew[i].Title);
}
uc.displayFindingSection(dt);
Then work out on that dt in displayFindingSection
Sorry if i am wrong...

Categories

Resources