Two key players in Silicon Valley’s battle to define the future of transportation are teaming up just as a common enemy faces new challenges.

Waymo, the self-driving division of Google’s parent company, and ride-hailing company Lyft are partnering to test driverless-car technology, the companies confirmed Sunday.

Both companies face a bitter foe in Uber and are seizing the moment as their rival stumbles. Uber, which is valued privately by investors at close to $70 billion, dwarfs Lyft in the ride-hailing arena, while Google is neck and neck with Uber in a race to develop autonomous vehicles.

“We can confirm that we are partnering with Waymo to safely and responsibly launch self-driving vehicle pilots,” a Lyft spokeswoman said in an email. “Waymo holds today’s best self-driving technology, and collaborating with them will accelerate our shared vision of improving lives with the world’s best transportation.”

Waymo also confirmed the partnership, which was first reported by the New York Times. “We’re looking forward to working with Lyft to explore new self-driving products that will make our roads safer and transportation more accessible,” a spokesman said in an emailed statement. “Lyft’s vision and commitment to improving the way cities move will help Waymo’s self-driving technology reach more people, in more places.”

Uber has faced ever-worsening setbacks that have made the seemingly unstoppable juggernaut vulnerable. The company is in a legal battle with Waymo over allegations that it is using stolen trade secrets for its self-driving car program. Last week district judge William Alsup referred the case to federal criminal prosecutors for investigation.

On Monday he dealt a further blow to Uber.

In documents issued late last week but unsealed Monday, judge Alsup did not suspend Uber's driverless-car program, but he gave Waymo broad power to scrutinize Uber's program.

A Waymo attorney and technical expert will be allowed to monitor Uber's ongoing business. They will also have the power to ask for a large number of documents that Uber has so far not handed over, including records of meetings, and phone calls with Anthony Levandowski, the engineer who took the documents. Uber must return the documents by May 31. Levandowski, who recently stepped aside as head of Uber's self-driving efforts, is now legally barred from working on anything related to LIDAR, the key self-driving technology that he is accused of stealing.

“The Waymo lawyers are now in a super-position to prove their case,” said Chris Broderick, an attorney focusing on trade secrets issues with Manatt, Phelps, and Phillips LLP.

Meanwhile, Lyft has been benefiting from a wave of consumers who have quit Uber in disgust following missteps during the Trump administration’s immigration ban, a sexual harassment scandal and a leaked video of Uber’s chief executive cursing at a driver. Waymo recently began to recruit Phoenix-area residents to join one of the largest efforts yet to put ordinary people inside self-driving cars.

The deal appears to play off the assets each company brings to the table. Lyft, which is privately valued at $7.5 billion, doesn’t have the resources to invest in self-driving car technology. The company has said it is not going to attempt to do so. But Lyft has a network of consumers who use the company’s app. Waymo, which is not consumer-facing, could benefit from that network as it races to bring the technology to the public.

The competition to develop autonomous vehicles is fierce, attracting the attention of traditional automakers as well as technology companies such as Apple. In pointing out Tesla's potential vulnerabilities, Morgan Stanley analyst Adam Jonas noted that, “There have been numerous developments that suggest to us the continued preparation of an assault by large tech firms on the market for shared, autonomous, electric mobility.”