0

I have been tasked with a selenium project where multiple websites have to be tested. The problem is that some times the elements are changed(eg. ID, Xpath, etc). So I wanted to lode the codes from a text file and execute it on the same instance of the application. A sample of how I wish to proceed:

    Initialization() 'Initializes the driver
    Dim objReader As New System.IO.StreamReader(FILE_NAME)' Reader to read from a partictlar file
    For i As Integer = 0 To ListBox1.Items.Count - 1
    ListBox2.SelectedIndex = i
    driver.Navigate.GoToUrl(ListBox1.SelectedIndex)
    Do While objReader.Peek() <> -1
    execute(objReader.ReadLine())' Run the line of code from the file.
    Loop
    objReader.Close()
    Next
    Termination()'Close the driver

Contents of the sample text file :

Dim SearchBox As IWebElement
SearchBox = GetWebElement(driver, By.XPath("/html/body/form/div/section/div/div[1]/div[3]/div[1]/input"), timeOut)
SearchBox.SendKeys(ListBox2.SelectedItem)

ref: Load VB.net code from .txt file and execute it on fly using System.CodeDom.Compiler

Import code from text VB.NET

May be there is a solution in the above links, a little guidance would be helpful.

Thanks.

Community
  • 1
  • 1
  • 1
    Dealing with changing elements is a common problem in web automation - it is an frequent approach to deal with it by storing the locators separately in a text config file instead, and load/apply them accordingly in runtime. But having the code logic adds an extra layer of unnecessary complexity (at least seemingly). Did you consider having only the locators in the text file, without any code? – skandigraun Mar 18 '16 at 11:05
  • You are right, may be i can store only the locators in the text file and then in the code I can get the type of locator and perform specific sendkeys or clicks. Switching frames will now become complicated. But is it not possible to handle the logic directly from the text file? – user2866752 Mar 18 '16 at 11:31
  • Well, everything is possible, but not everything worth the effort (with that said I'm not a VB guru, maybe it worth it in VB). But just as a high level example, I have the following in my mind: the text files contain the same configuration keys, with different values. If an element doesn't exist, its locator is empty. The code can check if the iframe value is empty or not. If it is not, switch to it. Otherwise simply continue without switching. And so on with other elements. – skandigraun Mar 18 '16 at 12:01
  • On the other hand you might want to check this question also, if you want to have the code from text file: http://stackoverflow.com/questions/14709263/import-code-from-text-vb-net?lq=1 – skandigraun Mar 18 '16 at 12:05
  • @skandigraun `code` builder.AppendLine(" Public Sub DoWork() Implements IScript.DoWork") builder.AppendLine(" Variable1 = ""Hello World""") builder.AppendLine(" End Sub") ; It is creating a sub and putting the code in that... What i intend to do is use it in the loop of a sub. – user2866752 Mar 18 '16 at 12:17
  • Or i could change the format of the script file and a lot of if else would solve the problem... but will have to tackle it only once. btw I have to work with 6 websites, and that is a real PITA. – user2866752 Mar 18 '16 at 12:35

0 Answers0