Forum Discussion

royd's avatar
royd
Regular Contributor
7 years ago
Solved

DDT.CurrentDriver.Next(); not working!

I am following the instruction here https://support.smartbear.com/testcomplete/docs/reference/program-objects/ddt/exceldriver.html

 

When running the script, it gets stuck at -

 

 

while (!DDT.CurrentDriver.EOF())
  {
    if (DDT.CurrentDriver.Value("Name") == "Tom Morgan")
    {
      newPatient.createPatients();

DDT.CurrentDriver.Next(); } }

 

 

Interestingly, while writing the script, by mistakenly put "DDT.CurrentDriver.Next();" outside the if statement. The script ran once! That is when I realized that it is in the wrong place. Correcting that caused it to stop working!

 

 

while (!DDT.CurrentDriver.EOF())
  {
    if (DDT.CurrentDriver.Value("Name") == "Tom Morgan")
    {
      newPatient.createPatients();

    } 
     DDT.CurrentDriver.Next();
  }

 

 

I can not figure out what I am doing wrong!

  • OK, I took a few minutes and spent time getting this to work.  THIS should work now...

     

    try
        {
          var xlDriver = DDT.ExcelDriver("C:\\femalePatients.xlsx", "patients", true);
          var startingRow = 2; //Let's say Lizzie is the second patient
          var rowsToProcess = 6;
          var currentRow = 0;
          while (!xlDriver.EOF())
          {
              currentRow++ //Increment this counter to mark what row you're on
              if((currentRow >= startingRow) && (rowsToProcess > 0)){
                  newPatient.createPatients();
                  rowsToProcess-- //decrement this counter to indicate you've processed the current row
              }
    
              if (rowsToProcess === 0){
                   break;
              }
              xlDriver.Next(); //Go to the next row
          } 
        } 
        
        catch(exception)//exception
        {
        Log.Error(exception.message);
        }
      DDT.CloseDriver(xlDriver.Name);
    }

    I'm building my own counters so the first row to attempt to process is row 1, not row 0.  My code above should skip the first row of actual data in your excel file then, starting with the second row of date, will create new patients for that row and the next 5 and then it should exit the loop. This worked for me in my test environment so it should work for you.

     

    EDIT: Note that the "rows" that I'm using have nothing directly to do with the driver itself... these are constructs that I created for tracking our way through the while loop and determining when to process and when to exit.

  • I see the problem... in newPatients.createPatient, you need to remove line nine where you define a new driver and then change all the xlDriver variables to DDT.CurrentDriver.  This is a legitimate way to use CurrentDriver.  You're calling "createPatient" within a data loop that's using a driver and you want to operate on the contents in the driver.  So... leave pDDT.js as it is... it's fine... and make the following changes to newPatient.js

     

    function createPatients(){
    
      var page = Sys.Browser("*").Page("*");
      
      
      var dept, admitDate, pName, gender, dob, ssn, email1, email2, lang, createPt;
      aqUtils.Delay(1000);
      dept = page.FindChild("ObjectIdentifier", "PAT_LOC", 10);
      admitDate = page.FindChild("ObjectIdentifier", "ADMITDATE", 10);
      pName = page.FindChild("ObjectIdentifier", "PATNAME", 10);  
      gender = page.FindChild("ObjectIdentifier", "PATSEX", 10);
      dob = page.FindChild("ObjectIdentifier", "PATDOB", 10);
      ssn = page.FindChild("ObjectIdentifier", "PATSSN", 10);
      email1 = page.FindChild("ObjectIdentifier", "PRIMARYADDRESS", 10);
      email2 = page.FindChild("ObjectIdentifier", "SECONDARYADDRESS", 10);
      lang = page.FindChild("ObjectIdentifier", "LANGUAGEPREF", 10);
      
      Log.AppendFolder(DDT.CurrentDriver.Value("Name"));
      
      dept.ClickItem(DDT.CurrentDriver.Value("Department"));
    
      admitDate.SetText(DDT.CurrentDriver.Value("Admission Date"));
    
      pName.SetText(DDT.CurrentDriver.Value("Name"));
    
      gender.ClickItem(DDT.CurrentDriver.Value("Sex"));
    
      dob.SetText(DDT.CurrentDriver.Value("DOB"));
    
      ssn.SetText(DDT.CurrentDriver.Value("SSN"));
    
      email1.SetText(DDT.CurrentDriver.Value("email1"));
    
      email2.SetText(DDT.CurrentDriver.Value("email2"));
    
      lang.ClickItem(DDT.CurrentDriver.Value("Lang"));
    
      createPt = page.FindChild("ObjectIdentifier", "submitBtn", 10);
      createPt.ClickButton();
    
      aqUtils.Delay(6000);  
      
    
      page.Keys("[F5]");
    
      Log.PopLogFolder();
      
    }
    
      module.exports.createPatients = createPatients;
      
      

16 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Within newPatient.createPatients, are you creating another DDT driver of some sort?  The thing about "CurrentDriver" is that it always returns the LAST used driver in the DDT object.  So, you created a driver for your while loop but as soon as you create another driver, CurrentDriver is now that driver.

     

    What I've found, especially if I'm playing with a lot of different DDT objects, is that it's better to assign the driver to a variable and reference that... like so.

     

    var myDriver = DDT.ExcelDriver('C:\\temp\\myfile.xlsx', 'MyTab', true);
    
    while (!myDriver.EOF())
      {
        if (myDriver.Value("Name") == "Tom Morgan")
        {
          newPatient.createPatients();
    
        } 
         myDriver.Next();
      }

    This way, in this code block, we're guarenteed to always use the proper driver.

    • royd's avatar
      royd
      Regular Contributor

      Hi Robert

       

      Thanks for replying. The newPatient.createPatients do not have any DDT driver, it only has steps to fill out patient information in the new patient form.

       

      Meanwhile, I will try as you suggested and let you know the result.

       

      Thank you.

       

      Regards

       

      Dave

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Keep in mind "Not Working" is a very vague note... what is not working?  Is it not doing a second loop? Is there an error message? What behavior are you expecting that you're not seeing.

         

        The code you posted basically says go through the DDT driver you have until it reaches the end.  The "Next" basically says that, each time through the loop, go to the next row.  Your If/Then logic compares the value in each row to see if it matches a specific value.  If it does, it creates a new patient. But no matter what the if/then logic does, it will ALWAYS loop.