How to transfer value from website to windows forms online? - selenium

I am creating an application on Windows Form and collided with a trouble.
I am getting each second a value from a website by Selenium Webdriver, using XPath and recording it to my local string. I am getting value of a timer. It doesn't matter, it's comfortable to me to get it like a string.
So. I have a loop, which reading data from website and this string I put to TextBox each second too.
But I am not able to see an output of the textBox until my loop is run. I don't want to stop it.
I want to see values from a website online in my application.
Do you have ideas how to refresh the form each second?
I can provide my code if you need.
Thank you in advance, my guru!
public static ChromeDriver GetDriver() {
return new ChromeDriver();
}
public static void CheckGame(string URLevent) {
while (GameIsRun) {
GetInformation(GetDriver(), URLevent);
GameIsRun = false;
}
}
static void GetInformation(ChromeDriver driver, string URLevent) {
driver.Navigate().GoToUrl(URLevent);
do {
TimeOfGame = driver.FindElement(By.XPath("html/body/div/div/div//div/" +
"div[#class='headroom-wrapper']/div//div[3]/div")).GetAttribute("innerHTML");
Print print = new Print()
print.Info(TimeOfGame);
System.Threading.Thread.Sleep(1000);
}
while (TimeOfGame != "90:00");
}
public partial class Form1: Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
Form form = new Form();
}
private static Form1 _form = null;
public static Form1 Form {
get {
if (_form == null) {
_form = new Form1();
}
return _form;
}
}
private void button1_Click(object sender, EventArgs e) {
string eventId = tbEventID.Text; //this works vice versa. It's not my issue.
string URLevent = "url..." + tbEventID.Text + "/";
Program.CheckGame(URLevent);
}
}
public class Print {
public void Info(string TimeOfGame) {
Form1.Form.tbTeam1.Text = TimeOfGame;
//my issue is here. I want to check result at TextBox each second!
}
}

This statement in your code
TimeOfGame = driver.FindElement(By.XPath("html/body/div/div/div//div/div[#class='headroom-wrapper']/div//div[3]/div")).GetAttribute("innerHTML");
is very brittle. You should use getText() method for extracting the inner text of HTML methods. Also, in order to locate webElement precisely, it is preferred to use focused relative Xpath over absolute Xpath.

Related

Revit Synchronization event

Starting with this...
https://github.com/jeremytammik/RevitLookup/blob/master/CS/EventTrack/Events/ApplicationEvents.cs
I'm trying to add an event listener for a synchronization event. the code below throws an error stating that the m_app is null. Can i not subscribe to this event while Revit is starting up?
I was able to do this before with application.ViewActivated += ..... Im wondering if this has something to do with DB vs UI driven events and when they are allowed to be subscribed to? I just don't know.
namespace RevitModelHealth
{
public class checkHealth : IExternalApplication
{
// Document doc;
static public Autodesk.Revit.ApplicationServices.Application m_app = null;
public Result OnShutdown(UIControlledApplication application)
{
return Result.Succeeded;
}
public Result OnStartup(UIControlledApplication application)
{
m_app.DocumentSynchronizingWithCentral += new EventHandler<DocumentSynchronizingWithCentralEventArgs>(m_app_DocumentSavingToCentral);
return Result.Succeeded;
}
void m_app_DocumentSavingToCentral(object sender, Autodesk.Revit.DB.Events.DocumentSynchronizingWithCentralEventArgs e)
{
MessageBox.Show("asd","asd");
}
}
}
Here is updated code reflecting my response to the first answer. The message box opens when the document is loaded. No errors are thrown when I try to initialize the synchronization event handlers, however, neither of the message boxes open before or after a synchronization event.
public class checkHealth : IExternalApplication
{
// Document doc;
static public Autodesk.Revit.ApplicationServices.Application m_app;
public Result OnShutdown(UIControlledApplication application)
{
return Result.Succeeded;
}
public Result OnStartup(UIControlledApplication application)
{
application.ControlledApplication.DocumentOpened += new EventHandler<DocumentOpenedEventArgs>(app_DocOpened);
return Result.Succeeded;
}
public void app_DocOpened(object sender, DocumentOpenedEventArgs args)
{
MessageBox.Show("asd","asd");
m_app.DocumentSynchronizingWithCentral += new EventHandler<DocumentSynchronizingWithCentralEventArgs>(m_app_DocumentSavingToCentral);
m_app.DocumentSynchronizedWithCentral += new EventHandler<Autodesk.Revit.DB.Events.DocumentSynchronizedWithCentralEventArgs>(m_app_DocumentSavedToCentral);
}
void m_app_DocumentSavingToCentral(object sender, Autodesk.Revit.DB.Events.DocumentSynchronizingWithCentralEventArgs e)
{
MessageBox.Show("sync", "sync");
}
void m_app_DocumentSavedToCentral(object sender, Autodesk.Revit.DB.Events.DocumentSynchronizedWithCentralEventArgs e)
{
MessageBox.Show("Doone", "Done");
}
}
this worked.... Thanks largely in part to the SDK sample project EventsMonitor
namespace RevitModelHealth
{
public class checkHealth : IExternalApplication
{
public Result OnShutdown(UIControlledApplication application)
{
return Result.Succeeded;
}
public Result OnStartup(UIControlledApplication application)
{
application.ControlledApplication.DocumentSynchronizingWithCentral += new EventHandler<DocumentSynchronizingWithCentralEventArgs>(app_syncStart);
application.ControlledApplication.DocumentSynchronizedWithCentral += new EventHandler<DocumentSynchronizedWithCentralEventArgs>(app_syncOver);
return Result.Succeeded;
}
public void app_syncStart(object o ,DocumentSynchronizingWithCentralEventArgs args)
{
MessageBox.Show("","Stasrting");
}
public void app_syncOver(object o,DocumentSynchronizedWithCentralEventArgs args)
{
MessageBox.Show("", "Over");
}
}
}
Try
application.ControlledApplication.DocumentSynchronizingWithCentral += new EventHandler<DocumentSynchronizingWithCentralEventArgs>(m_app_DocumentSavingToCentral)
in your OnStartup() method.
The call is failing because instance member m_app is initialized to null.
The UIApplication.ControlledApplication object that raises the DocumentSynchronizingWithCentralEventArgs is being accessible from the parameter to OnStartup.
You can try this:
public void app_DocOpened(object sender, DocumentOpenedEventArgs args)
{
MessageBox.Show("asd","asd");
Autodesk.Revit.ApplicationServices.Application m_app = args.Document.Application;
m_app.DocumentSynchronizingWithCentral += new EventHandler<DocumentSynchronizingWithCentralEventArgs>(m_app_DocumentSavingToCentral);
m_app.DocumentSynchronizedWithCentral += new EventHandler<Autodesk.Revit.DB.Events.DocumentSynchronizedWithCentralEventArgs>(m_app_DocumentSavedToCentral);
}

Randomize User agent GeckoFX

is there a way of randomizing the user agent using Gecko Browser ? i tried doing it on a separated thread but i couldn't since Gecko must be ran on the same Thread.
try this
Create new class Global.cs
public class Globals
{
public static ArrayList Useragent = new ArrayList();
}
next in your form1 code
private string GetUserAgent()
{
Random Rnd = new Random();
return Convert.ToString(Globals.Useragent[Rnd.Next(0, Globals.Useragent.Count)]);
}
load your file with user agent lines
private void button2_Click(object sender, EventArgs e)
{
var OpenFile = new OpenFileDialog();
OpenFile.Filter = "*.txt | *.txt";
OpenFile.ShowDialog();
if (OpenFile.FileName != "")
{
Globals.Useragent.AddRange(File.ReadAllLines(OpenFile.FileName));
}
else
{
MessageBox.Show("Chooee Your User agent file");
}
}
======================
private string GetUserAgent()
{
Random Rnd = new Random();
return Convert.ToString(Globals.Useragent[Rnd.Next(0, Globals.Useragent.Count)]);
}
Ok its finish!
Now you can do this - input your new code GetUserAgent();
private void button1_Click(object sender, EventArgs e)
{
CookieManager.RemoveAll();
Gecko.GeckoPreferences.User["general.useragent.override"] = GetUserAgent();
}

Extend DropDownList to add ListSearchExtender

I want to extend DropDownList to add ListSearchExtender.
Using the code below, the control works well in runtime but in design time it give me this error:
SearchDropDownList - DdlTest There was an error rendering the
control. Page cannot be null. Please ensure that this operation is
being performed in the context of an ASP.NET request.
I'd like to understand the cause of this error.
[ToolboxData("<{0}:SearchDropDownList runat=\"server\"></{0}:SearchDropDownList>")]
public class SearchDropDownList : DropDownList
{
private ListSearchExtender listSearchExt = new ListSearchExtender();
protected override void OnInit(EventArgs e)
{
ReloadSettings();
}
protected override void Render(HtmlTextWriter w)
{
base.Render(w);
listSearchExt.RenderControl(w);
}
public void ReloadSettings()
{
listSearchExt.TargetControlID = this.ID;
listSearchExt.ID = this.ID + "_CalendarExtender";
if (Controls.Count > 0)
{
foreach (Control item in Controls)
{
if (item.ID == listSearchExt.ID)
{
Controls.Remove(item);
}
}
}
Controls.Add(listSearchExt);
}
}
i got it by simple way i am not sure if it will make problem in future but for now it work well
protected override void Render(HtmlTextWriter w)
{
base.Render(w);
if (!this.DesignMode)
{
listSearchExt.RenderControl(w);
}
}

GWT popup is not centered when built within onClickHandler

My aim is to use GWT.runSync to load the popup contents only when required.
If I construct my widget as:
public class CreateButton extends Button {
public CreateButton() {
super("Create");
buildUI();
}
private void buildUI() {
final CreateWidget createWidget = new CreateWidget();
final PopupPanel popupPanel = new PopupPanel(false);
popupPanel.setWidget(createWidget);
popupPanel.setGlassEnabled(true);
popupPanel.setAnimationEnabled(true);
addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
popupPanel.center();
}
});
}
}
Then the popup will be centered correctly.
If I build the popup within the clickHandler:
public class CreateButton extends Button {
public CreateButton() {
super("Create");
buildUI();
}
private void buildUI() {
#Override
public void onClick(ClickEvent event) {
final CreateWidget createWidget = new CreateWidget();
final PopupPanel popupPanel = new PopupPanel(false);
popupPanel.setWidget(createWidget);
popupPanel.setGlassEnabled(true);
popupPanel.setAnimationEnabled(true);
addClickHandler(new ClickHandler() {
popupPanel.center();
}
});
}
}
The popup will not center correctly. I have tried using setPositionAndShow, however the supplied offsets are 12, even though the CreateWidget is actually about 200px for both width and height.
I want to use the second method so I can eventually use GWT.runAsync within the onClick as CreateWidget is very complex.
I am using GWT-2.1.1
Seems to work by delaying the call to center. Perhaps a once off Timer would work as well. Delaying the call also works when wrapping buildUI within GWT.runAsync
public class CreateButton extends Button {
public CreateButton() {
super("Create");
buildUI();
}
private void buildUI() {
#Override
public void onClick(ClickEvent event) {
final CreateWidget createWidget = new CreateWidget();
final PopupPanel popupPanel = new PopupPanel(false);
popupPanel.setWidget(createWidget);
popupPanel.setGlassEnabled(true);
popupPanel.setAnimationEnabled(true);
addClickHandler(new ClickHandler() {
Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {
#Override
public boolean execute() {
popupPanel.center();
return false;
}
}, 50); //a value greater than 50 maybe needed here.
});
}
}
}
}

How to repeat a the windows form (Tab Function)

Using VB.Net
I am having the form name as "Employee Entry"
User want to open a Employee Entry from in 5 tabs...
For Example
When the user open a Employee Entry form, while entering the details at that same time, user again open a employee entry form (2nd Employee entry form show like a new employee entry from with out any details)
More Example
We can open Google, Yahoo, in one browser (by using Tab Function).
How to make this.
Need vb.net code Help
I'm assuming this is WinForms? You should design your Employee Entry as a User Control. You can then add a new instance of that control to your Form on each request of a new Employee. You would most likely have a seperate Employee object that you could pass to each instance of your EmployeeEntry user control.
EDIT1
Create a form with a menu strip across the top and a menu item called tsmiNewEmployee. Add a tab control to the form named tabEntryForms and set its fill style to Dock. Delete all default tab pages from it.
Add Class:
public class EmployeeRecord
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime HireDate { get; set; }
}
Add USerControl named EmployeeEntry. Puts osme Firts/Last name edit cotrols on it, etc. Add a Save button and a Cancel Button.
public partial class EmployeeEntry : UserControl
{
public event EventHandler Save;
public event EventHandler Cancel;
private EmployeeRecord empBeingEdited = null;
public EmployeeRecord Employee
{
get
{
return empBeingEdited;
}
set
{
empBeingEdited = value;
if (empBeingEdited == null)
{
// clear controls
}
else
{
// populate controls with employee values
}
}
}
public EmployeeEntry()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
if (Save != null)
Save(this, EventArgs.Empty);
}
private void btnCancel_Click(object sender, EventArgs e)
{
if (Cancel != null)
Cancel(this, EventArgs.Empty);
}
}
And in your main form that has the tab control:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void tsmiNewEmployee_Click(object sender, EventArgs e)
{
TabPage tab = new TabPage("New Employee Entry");
EmployeeEntry empEntry = new EmployeeEntry();
empEntry.Employee = new EmployeeRecord();
empEntry.Save += new EventHandler(EmployeeEntry_Save);
empEntry.Cancel += new EventHandler(EmployeeEntry_Cancel);
tab.Controls.Add(empEntry);
empEntry.Dock = DockStyle.Fill;
tabEntryForms.TabPages.Add(tab);
}
private void EmployeeEntry_Save(object source, EventArgs args)
{
EmployeeEntry empEntry = source as EmployeeEntry;
MessageBox.Show("Saved");
TabPage parentTab = empEntry.Parent as TabPage;
parentTab.Controls.Remove(empEntry);
tabEntryForms.Controls.Remove(parentTab);
}
private void EmployeeEntry_Cancel(object source, EventArgs args)
{
EmployeeEntry empEntry = source as EmployeeEntry;
MessageBox.Show("NOT Saved");
TabPage parentTab = empEntry.Parent as TabPage;
parentTab.Controls.Remove(empEntry);
tabEntryForms.Controls.Remove(parentTab);
}
}
That should get you started.